def train_(base_path): data, anno_viable, anno_dead = read_data(base_path) print("loaded!!") anno_viable = np.expand_dims(anno_viable, axis=-1) anno_dead = np.expand_dims(anno_dead, axis=-1) print("expanded!!") mean = np.mean(data) print("mean finished!!") std = np.std(data) print("std finished!!") data_ = (data - mean) / std train_data = data_[:50] train_anno_viable = anno_viable[:50] train_anno_dead = anno_dead[:50] val_data = data_[50:] val_anno_viable = anno_viable[50:] val_anno_dead = anno_dead[50:] print('-' * 30) print( 'Creating and compiling the fully convolutional regression networks.') print('-' * 30) if sys.argv[1] == 'unet': model = buildModel_U_net(input_dim=(504, 376, 3)) filename = 'cell_counting_viable_unet.hdf5' elif sys.argv[1] == 'fcrna': model = buildModel_FCRN_A_v2(input_dim=(504, 376, 3)) filename = 'cell_counting_viable_fcrna.hdf5' else: raise ValueError( 'The first command line argument should be "unet" or "fcrna"') learn(filename, train_data, train_anno_viable, val_data, val_anno_viable, model) if sys.argv[1] == 'unet': model = buildModel_U_net(input_dim=(504, 376, 3)) filename = 'cell_counting_dead_unet.hdf5' elif sys.argv[1] == 'fcrna': model = buildModel_FCRN_A_v2(input_dim=(504, 376, 3)) filename = 'cell_counting_dead_fcrna.hdf5' else: raise ValueError( 'The first command line argument should be "unet" or "fcrna"') learn(filename, train_data, train_anno_dead, val_data, val_anno_dead, model)
def train_(base_path): data, anno = read_data(base_path) anno = np.expand_dims(anno, axis = -1) mean = np.mean(data) std = np.std(data) data_ = (data - mean) / std train_data = data_[:150] train_anno = anno[:150] val_data = data_[150:] val_anno = anno[150:] print('-'*30) print('Creating and compiling the fully convolutional regression networks.') print('-'*30) model = buildModel_U_net(input_dim = (256,256,3)) model_checkpoint = ModelCheckpoint('cell_counting.hdf5', monitor='loss', save_best_only=True) model.summary() print('...Fitting model...') print('-'*30) change_lr = LearningRateScheduler(step_decay) datagen = ImageDataGenerator( featurewise_center = False, # set input mean to 0 over the dataset samplewise_center = False, # set each sample mean to 0 featurewise_std_normalization = False, # divide inputs by std of the dataset samplewise_std_normalization = False, # divide each input by its std zca_whitening = False, # apply ZCA whitening rotation_range = 30, # randomly rotate images in the range (degrees, 0 to 180) width_shift_range = 0.3, # randomly shift images horizontally (fraction of total width) height_shift_range = 0.3, # randomly shift images vertically (fraction of total height) zoom_range = 0.3, shear_range = 0., horizontal_flip = True, # randomly flip images vertical_flip = True, # randomly flip images fill_mode = 'constant', dim_ordering = 'tf') # Fit the model on the batches generated by datagen.flow(). model.fit_generator(datagen.flow(train_data, train_anno, batch_size = 16 ), samples_per_epoch = train_data.shape[0], nb_epoch = 192, callbacks = [model_checkpoint, change_lr], ) model.load_weights('cell_counting.hdf5') A = model.predict(val_data) mean_diff = np.average(np.abs(np.sum(np.sum(A,1),1)-np.sum(np.sum(val_anno,1),1))) / (100.0) print('After training, the difference is : {} cells per image.'.format(np.abs(mean_diff)))
# train_data = data_[:12] train_data = (X_train - np.mean(X_train)) / np.std(X_train) train_anno = anno[:len(train_set)] # val_data = data_[12:] val_data = (X_test - np.mean(X_test)) / np.std(X_test) val_anno = anno[len(train_set):] return train_data, train_anno, val_data, val_anno train_data, train_anno, val_data, val_anno = load_data() #%% Creat the model model = buildModel_U_net(input_dim=train_data.shape[1:]) model.load_weights('trained_model.hdf5') #%% Detection def detect(data=val_data, threshold=0.6): start = time.time() A = model.predict(val_data) print("\nConsumed time: \t%.2f\t s\n" % (time.time() - start)) #mean_diff = np.average(np.abs(np.sum(np.sum(A,1),1)-np.sum(np.sum(val_anno,1),1))) / (100.0) #print('After training, the difference is : {} cells per image.'.format(np.abs(mean_diff))) preds_test = np.where(A > 0, A / 100, A) preds_test = (preds_test + 1) / 2
from tqdm import tqdm #%% # Set some parameters IMG_WIDTH = 1024 IMG_HEIGHT = 512 IMG_CHANNEL = 3 seed = 42 random.seed = seed np.random.seed = seed #%% Creat the model print('#' * 30) print('Creating and compiling the fully convolutional regression networks.') print('#' * 30) model = buildModel_U_net(input_dim=(IMG_HEIGHT, IMG_WIDTH, IMG_CHANNEL)) model.load_weights('trained_model.hdf5') print('#' * 30) print('Model loaded.') print('#' * 30) #%% Read image def read_image(img_path): img_name = os.path.basename(img_path)[:-4] img = cv2.imread(img_path) img = cv2.resize(img, (IMG_WIDTH, IMG_HEIGHT), interpolation=cv2.INTER_AREA) img = np.asarray(img)