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
mdl.load_weights('weight_center.h5') print('The model loading time is %s seconds' % (time.time() - start_time)) 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)
nb_epoch = 12 # number of convolutional filters to use nb_filters = 32 # size of pooling area for max pooling nb_pool = 2 # convolution kernel size nb_conv = 3 fname = '../../test/test_data/1038.tiff' ind_uncenter1 = range(1038, 1047) ind_uncenter2 = range(1049, 1057) uncenter1 = dxchange.read_tiff_stack(fname, ind=ind_uncenter1, digit=4) uncenter2 = dxchange.read_tiff_stack(fname, ind=ind_uncenter2, digit=4) uncenter = np.concatenate((uncenter1, uncenter2), axis=0) uncenter = nor_data(uncenter) print(uncenter.shape) uncenter = img_window(uncenter[:, 360:1460, 440:1440], 200) print(uncenter.shape) uncenter_patches = extract_3d(uncenter, patch_size, 1) np.random.shuffle(uncenter_patches) print(uncenter_patches.shape) # print uncenter_patches.shape center_img = dxchange.read_tiff('../../test/test_data/1048.tiff') center_img = nor_data(center_img) print(center_img.shape) center_img = img_window(center_img[360:1460, 440:1440], 400) center_patches = extract_3d(center_img, patch_size, 1) np.random.shuffle(center_patches) print(center_patches.shape) # plt.imshow(center_img, cmap='gray', interpolation= None)
nb_epoch = 12 # number of convolutional filters to use nb_filters = 32 # size of pooling area for max pooling nb_pool = 2 # convolution kernel size nb_conv = 3 fname = '../../test/test_data/1038.tiff' ind_uncenter1 = range(1038, 1047) ind_uncenter2 = range(1049, 1057) uncenter1 = dxchange.read_tiff_stack(fname, ind=ind_uncenter1, digit=4) uncenter2 = dxchange.read_tiff_stack(fname, ind=ind_uncenter2, digit=4) uncenter = np.concatenate((uncenter1, uncenter2), axis=0) uncenter = nor_data(uncenter) print (uncenter.shape) uncenter = img_window(uncenter[:, 360:1460, 440:1440], 200) print (uncenter.shape) uncenter_patches = extract_3d(uncenter, patch_size, 1) np.random.shuffle(uncenter_patches) print (uncenter_patches.shape) # print uncenter_patches.shape center_img = dxchange.read_tiff('../../test/test_data/1048.tiff') center_img = nor_data(center_img) print (center_img.shape) center_img = img_window(center_img[360:1460, 440:1440], 400) center_patches = extract_3d(center_img, patch_size, 1) np.random.shuffle(center_patches) print (center_patches.shape) # plt.imshow(center_img, cmap='gray', interpolation= None)
nb_evl = 100 start_time = time.time() fnames = glob.glob('../../test/test_data/*.tiff') fnames = np.sort(fnames) mdl = model(dim_img, nb_filters, nb_conv, nb_classes) mdl.load_weights('classify_training_weights.h5') print('The model loading time is %s seconds'%(time.time()-start_time)) start_time = time.time() Y_score = np.zeros((len(fnames))) for i in range(len(fnames)): img = dxchange.read_tiff(fnames[i]) img = nor_data(img) X_evl = np.zeros((nb_evl, dim_img, dim_img)) for j in range(nb_evl): X_evl[j] = img_window(img[360:1460, 440:1440], dim_img) 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] = sum(np.dot(Y_evl, [0, 1])) #print('The evaluate score is:', Y_score[i]) #Y_score = sum(np.round(Y_score))/len(Y_score) ind_max = np.argmax(Y_score) print('The well-centered reconstruction is:', fnames[ind_max]) print('The prediction runs for %s seconds'%(time.time()-start_time)) plt.plot(Y_score)
mdl.load_weights('weight_center.h5') print('The model loading time is %s seconds'%(time.time()-start_time)) 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])