def train(img_x, img_y, patch_size, patch_step, dim_img, nb_filters, nb_conv, batch_size, nb_epoch): """ Function description. Parameters ---------- parameter_01 : type Description. parameter_02 : type Description. parameter_03 : type Description. Returns ------- return_01 Description. """ img_x = utils.nor_data(img_x) img_y = utils.nor_data(img_y) img_input = utils.extract_patches(img_x, patch_size, patch_step) img_output = utils.extract_patches(img_y, patch_size, patch_step) img_input = np.reshape(img_input, (len(img_input), 1, dim_img, dim_img)) img_output = np.reshape(img_output, (len(img_input), 1, dim_img, dim_img)) mdl = model(dim_img, nb_filters, nb_conv) mdl.fit(img_input, img_output, batch_size=batch_size, nb_epoch=nb_epoch) return mdl
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
Y_score = np.zeros((50, 501)) for i in range(50): slice_num = (i+2)*20 datapath = '/home/oxygen/YANGX/Globus/center/test_04/slice'+str(slice_num)+'/*.tiff' # print(datapath) fnames = glob.glob(datapath) fnames = np.sort(fnames) # print(fnames) for j in range(len(fnames)): img = dxchange.read_tiff(fnames[j]) img = -nor_data(img) # X_evl = np.zeros((nb_evl, dim_img, dim_img)) # for k in range(nb_evl): # X_evl[k] = img_window(img[360:1660, 440:1640], dim_img) X_evl = extract_patches(img[360:1660, 440:1640], (128, 128), step=64, max_patches=None, random_state=None) X_evl = X_evl.reshape(X_evl.shape[0], 1, dim_img, dim_img) Y_evl = mdl.predict(X_evl, batch_size=batch_size) Y_score[i, j] = sum(np.dot(Y_evl, [0, 1])) # print(Y_score[i]) #print('The evaluate score is:', Y_score[i]) #Y_score = sum(np.round(Y_score))/len(Y_score) ind_max = np.argmax(Y_score[i, :]) print('The well-centered reconstruction is:', fnames[ind_max]) # print('The prediction runs for %s seconds'%(time.time()-start_time)) # plt.plot(Y_score[i, :]) # plt.show() np.save('Y_score', Y_score)
slice_num = (i + 2) * 20 datapath = '/home/oxygen/YANGX/Globus/center/test_04/slice' + str( slice_num) + '/*.tiff' # print(datapath) fnames = glob.glob(datapath) fnames = np.sort(fnames) # print(fnames) for j in range(len(fnames)): img = dxchange.read_tiff(fnames[j]) img = -nor_data(img) # X_evl = np.zeros((nb_evl, dim_img, dim_img)) # for k in range(nb_evl): # X_evl[k] = img_window(img[360:1660, 440:1640], dim_img) X_evl = extract_patches(img[360:1660, 440:1640], (128, 128), step=64, max_patches=None, random_state=None) X_evl = X_evl.reshape(X_evl.shape[0], 1, dim_img, dim_img) Y_evl = mdl.predict(X_evl, batch_size=batch_size) Y_score[i, j] = sum(np.dot(Y_evl, [0, 1])) # print(Y_score[i]) #print('The evaluate score is:', Y_score[i]) #Y_score = sum(np.round(Y_score))/len(Y_score) ind_max = np.argmax(Y_score[i, :]) print('The well-centered reconstruction is:', fnames[ind_max]) # print('The prediction runs for %s seconds'%(time.time()-start_time)) # plt.plot(Y_score[i, :]) # plt.show()