def predict(mdl, img, patch_size, patch_step, batch_size, dim_img): """ the cnn model for image transformation Parameters ---------- img : array The image need to be calculated patch_size : (int, int) The patches dimension dim_img : int The input image dimension Returns ------- img_rec Description. """ img = np.float16(utils.nor_data(img)) img_y, img_x = img.shape x_img = utils.extract_patches(img, patch_size, patch_step) x_img = np.reshape(x_img, (len(x_img), 1, dim_img, dim_img)) y_img = mdl.predict(x_img, batch_size=batch_size) del x_img y_img = np.reshape(y_img, (len(y_img), dim_img, dim_img)) img_rec = utils.reconstruct_patches(y_img, (img_y, img_x), patch_step) return img_rec
def predict(mdl, img, patch_size, patch_step, batch_size, dim_img): """ the cnn model for image transformation Parameters ---------- img : array The image need to be calculated patch_size : (int, int) The patches dimension dim_img : int The input image dimension Returns ------- img_rec Description. """ img = np.float16(utils.nor_data(img)) img_y, img_x = img.shape x_img = utils.extract_patches(img, patch_size, patch_step) x_img = np.reshape(x_img, (len(x_img), 1, dim_img, dim_img)) y_img = mdl.predict(x_img, batch_size=batch_size) del x_img y_img = np.reshape(y_img, (len(y_img), dim_img, dim_img)) img_rec = utils.reconstruct_patches(y_img, (img_y, img_x), patch_step) return img_rec
def predict(mdl, img, patch_size, patch_step, batch_size, dim_img): """ the cnn model for image transformation Parameters ---------- img : array The image need to be calculated patch_size : (int, int) The patches dimension dim_img : int The input image dimension Returns ------- img_rec Description. """ img = np.float16(utils.nor_data(img)) img_h, img_w = img.shape input_img = utils.extract_patches(img, patch_size, patch_step) input_img = np.reshape(input_img, (input_img.shape[0], dim_img, dim_img, 1)) output_img = mdl.predict(input_img, batch_size=batch_size) del input_img output_img = np.reshape(output_img, (output_img.shape[0], dim_img, dim_img)) img_rec = utils.reconstruct_patches(output_img, (img_h, img_w), patch_step) return img_rec
def pred_single(mdl, predict_x, ih, iw, patch_shape, patch_step, patch_size, batch_size): predict_x = extract_3d(predict_x, patch_shape, patch_step) predict_x = np.reshape(predict_x, (predict_x.shape[0], patch_size, patch_size, 1)) predict_y = mdl.predict(predict_x, batch_size=batch_size) predict_y = np.reshape(predict_y, (predict_y.shape[0], patch_size, patch_size)) predict_y = reconstruct_patches(predict_y, (ih, iw), patch_step) return predict_y
def seg_predict(img, wpath, patch_size = 32, patch_step = 1, nb_conv=32, size_conv=3, batch_size=1000, nb_down=2, nb_gpu = 1): """ Function description Parameters ---------- img : array The images need to be segmented. wpath: string The path where the trained weights of the model can be read. patch_size: int The size of the small patches extracted from the input images. This size should be big enough to cover the features of the segmentation object. patch_step: int The pixel steps between neighbour patches. Larger steps leads faster speed, but less quality. I recommend 1 unless you need quick test of the algorithm. nb_conv: int Number of the covolutional kernals for the first layer. This number doubles after each downsampling layer. size_conv: int Size of the convolutional kernals. batch_size: int Batch size for the training. Bigger size leads faster speed. However, it is restricted by the memory size of the GPU. If the user got the memory error, please decrease the batch size. nb_epoch: int Number of the epoches for the training. It can be understand as the number of iterations during the training. Please define this number as the actual convergence for different data. nb_down: int Number of the downsampling for the images in the model. nb_gpu: int Number of GPUs you want to use for the training. Returns ------- save the segmented images to the spath. """ patch_shape = (patch_size, patch_size) img = np.float32(nor_data(img)) mdl = model_choose(patch_size, patch_size, nb_conv, size_conv, nb_down, nb_gpu) # print(mdl.summary()) mdl.load_weights(wpath) if img.ndim == 2: ih, iw = img.shape predict_x = extract_3d(img, patch_shape, patch_step) predict_x = np.reshape(predict_x, (predict_x.shape[0], patch_size, patch_size, 1)) predict_y = mdl.predict(predict_x, batch_size=batch_size) predict_y = np.reshape(predict_y, (predict_y.shape[0],patch_size, patch_size)) predict_y = reconstruct_patches(predict_y, (ih, iw), patch_step) return predict_y else: pn, ih, iw = img.shape images = np.empty(pn, dtype=object) # create empty array for i in range(pn): print('Processing the %s th image' % i) tstart = time.time() predict_x = img[i] predict_x = extract_3d(predict_x, patch_shape, patch_step) predict_x = np.reshape(predict_x, (len(predict_x), patch_size, patch_size, 1)) predict_y = mdl.predict(predict_x, batch_size=batch_size) predict_y = np.reshape(predict_y, (len(predict_y), patch_size, patch_size)) predict_y = reconstruct_patches(predict_y, (ih, iw), patch_step) images[i]=predict_y return images