def cache_train(bands): train_shapes = shapes[~shapes['image_id'].isin(train_wkt['ImageId'].unique( ))] print('num_train_images = ', train_shapes.shape[0]) min_train_height = train_shapes['height'].min() min_train_width = train_shapes['width'].min() # num_train = train_shapes.shape[0] num_train = 50 image_rows = min_train_height image_cols = min_train_width num_channels = bands num_mask_channels = 11 f = h5py.File(os.path.join(data_path, 'unlabeled_train_3.h5'), 'w') imgs = f.create_dataset('train', (num_train, num_channels, image_rows, image_cols), dtype=np.float16) # imgs_mask = f.create_dataset('train_mask', (num_train, num_mask_channels, image_rows, image_cols), dtype=np.uint8) ids = [] i = 0 for image_id in tqdm(sorted(train_shapes['image_id'].unique())): # drop the test image if image_id == '6110_3_1': continue if image_id in informative_set: image = extra_functions.read_image(image_id, bands) _, height, width = image.shape imgs[i] = image[:, :min_train_height, :min_train_width] # imgs_mask[i] = read_mask(image_id, height, width)[:, :min_train_height, :min_train_width] / 255 ids += [image_id] i += 1 if i == 36: break # fix from there: https://github.com/h5py/h5py/issues/441 f['train_ids'] = np.array(ids).astype('|S9') f.close()
def cache_train(): fp_files = [] for (dirpath, dirnames, filenames) in walk(data_path): fp_files.extend(filenames) break print("Number of Training images: ", len(fp_files)) print("Processing...") num_channels = 3 num_mask_channels = 1 image_rows = 3705 image_cols = 4800 num_train = len(fp_files) f = h5py.File(os.path.join(data_path, 'train_16.h5'), 'w') imgs = f.create_dataset('train', (num_train, image_rows, image_cols, num_channels), dtype=np.float16) imgs_mask = f.create_dataset( 'train_mask', (num_train, image_rows, image_cols, num_mask_channels), dtype=np.uint8) ids = [] i = 0 for image_id in sorted(fp_files): print(image_id) image = extra_functions.read_image(image_id) mask = extra_functions.read_mask(image_id) height, width, _ = image.shape imgs[i] = image imgs_mask[i] = mask ids += [image_id] i += 1 # fix from there: https://github.com/h5py/h5py/issues/441 f['train_ids'] = np.array(ids).astype('|S9') f.close() print("Training Images Cached Successfully.")
t[t > b] = b out[:, :, i] = t return out.astype(np.uint8) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.gpu_options.visible_device_list = gpu_id sess = tf.Session(config=config) for image_id in test_ids: # Print image name print(image_id) # Read in the testing image image = read_image(image_id, bands=input_channels) c, h, w = image.shape h = int(int(h / img_rows) * img_rows) w = int(int(w / img_cols) * img_cols) # Make prediction ( Also for flip and swap_axis) predicted_mask_p1 = make_prediction(model, model_path, image, input_size=(img_rows, img_cols), crop=cropping, num_masks=output_classes, num_channels=input_channels, sess=sess) predicted_mask_p2 = make_prediction(model, model_path, image[:, int(img_rows/2):h-int(img_rows/2), int(img_cols/2):w-int(img_cols/2)], input_size=(img_rows, img_cols), crop=cropping, num_masks=output_classes, num_channels=input_channels, sess=sess) p2_mask = np.zeros((11, img_rows, img_cols)) for i in range(img_rows): if i < img_rows / 2 - 1: j = int(img_cols / 2) - 2 - i
def __init__(self, image_ids, img_rows, img_cols, num_channels, cropping, horizontal_flip=False, vertical_flip=False, swap_axis=False, mask=-1): self.CLASSES = { 1: 'Building', 2: 'Structure', 3: 'Road', 4: 'Track', 5: 'Trees', 6: 'Crops', 7: 'Fast_H20', 8: 'Slow_H20', 9: 'Truck', 10: 'Car', 11: 'Background' } self.image_ids = image_ids train_wkt = pd.read_csv(os.path.join('../data', 'train_wkt_v4.csv')) shapes = pd.read_csv(os.path.join('../data', '3_shapes.csv')) train_shapes = shapes[shapes['image_id'].isin( train_wkt['ImageId'].unique())] min_train_height = train_shapes['height'].min() min_train_width = train_shapes['width'].min() self.y_train = np.zeros((24, 1, min_train_height, min_train_width)) self.X_train = np.zeros((24, 3, min_train_height, min_train_width)) for i in range(len(image_ids)): image_id = image_ids[i] # X_train image = extra_functions.read_image(image_id, 3) self.X_train[:] = image[:, :min_train_height, :min_train_width] # y_train _, height, width = image.shape self.y_train[:] = self.read_mask( image_id, height, width, mask)[:, :min_train_height, :min_train_width] / 255 print("Loaded image {}".format(str(i))) self.y_train = np.reshape(self.y_train, [24, 1, min_train_height, min_train_width]) self.horizontal_flip = horizontal_flip self.vertical_flip = vertical_flip self.swap_axis = swap_axis self.num_channels = num_channels self.img_rows = img_rows self.img_cols = img_cols self.cropping = cropping if mask == -1: # set this value as variable later self.num_mask_channels = 11 else: self.num_mask_channels = 1
num_channels=input_channels, cropping=cropping, horizontal_flip=horizontal_flip, vertical_flip=vertical_flip, swap_axis=swap_axis, mask=mask, unlabeled=True) # initialize trainer trainer = Trainer(model, batch_size=batch_size, verification_batch_size=veri_batch_size, norm_grads=False, optimizer=optimizer, mask=mask, test_x=read_image(veri_image_id, input_channels), test_y=read_GT_Masks(veri_image_id, output_classes, mask), save_image_dims=save_image_dims, opt_kwargs=op_param) # start training trainer.train( data, unlabeled_data, model_save_path, training_iters=batch_pass, epochs=nb_epoch, dropout=dropout, # probability to keep units display_step=display_step, restore=restore,