def get_imgs(img_name, input_path, output_path, input_shape, list_classes, nbr_classes): label_name = img_name.replace(".jpg", ".png") batch_img = np.zeros(shape=(1, input_shape[0], input_shape[1], 3)) batch_label = np.zeros(shape=(1, input_shape[0], input_shape[1], nbr_classes)) preprocessor_inst = InstanceToOneHot(class_order=list_classes) preprocessor_one_hot = OneHotEncoding(total_number_classes=nbr_classes) image = cv2.imread(os.path.join(input_path, img_name)) label = cv2.imread(os.path.join(output_path, label_name), 0) resized_image = cv2.resize(image, (input_shape[1], input_shape[0])) resized_label = cv2.resize(label, (input_shape[1], input_shape[0]), interpolation=cv2.INTER_NEAREST) index_based_label, _ = preprocessor_inst.pre_process(resized_label) one_hot_label, _ = preprocessor_one_hot.pre_process(index_based_label) batch_img[0] = resized_image batch_label[0] = one_hot_label return batch_img, batch_label
def data_generator(input_path, output_path, batch_size, input_shape, list_classes, nbr_classes): # Create list of directory img_list = os.listdir(input_path) img_indices = list(range(len(img_list))) # Initialize the pre-process classes preprocessor_inst = InstanceToOneHot(class_order=list_classes) preprocessor_one_hot = OneHotEncoding(total_number_classes=nbr_classes) # Pre-process output labels for the batches while True: # Indexes for batch # batch_indices = np.random.randint(low=0, high=len(img_list), size=batch_size) batch_indices = random.sample(img_indices, batch_size) for delete_ind in batch_indices: img_indices.remove(delete_ind) if len(img_indices) < batch_size: print(" Nice, indices reset for next epoch!") img_indices = list(range(len(img_list))) elif len(img_indices) is None: print("Nice") img_indices = list(range(len(img_list))) # initialize batch_img and batch_labels to the correct shape batch_img = np.zeros(shape=(batch_size, input_shape[0], input_shape[1], 3)) batch_label = np.zeros(shape=(batch_size, input_shape[0], input_shape[1], nbr_classes)) for i in range(batch_size): # Extract image name and generate it's corresponding label name img_name = img_list[batch_indices[i]] label_name = img_name.replace(".jpg", ".png") # Read image and label label = cv2.imread(os.path.join(output_path, label_name), 0) image = cv2.imread(os.path.join(input_path, img_name)) # Resize image and label new_image = cv2.resize(image, (input_shape[1], input_shape[0])) new_label = cv2.resize(label, (input_shape[1], input_shape[0]), interpolation=cv2.INTER_NEAREST) # Call pre-processes # new_label = preprocessor_class.pre_process(new_label) new_label, _ = preprocessor_inst.pre_process(new_label) new_label, _ = preprocessor_one_hot.pre_process(new_label) # Add updated image and label to the return parameters batch_img[i] = new_image batch_label[i] = new_label yield batch_img, batch_label
def get_imgs_and_depth(img_name, input_path, output_path, depth_path, input_shape, list_classes, nbr_classes): # PYDNET_SAVED_WEIGHTS = '/WeightModels/exjobb/OldStuff/pydnet_weights/pydnet_org/pydnet' label_name = img_name.replace(".jpg", ".png") batch_img = np.zeros(shape=(1, input_shape[0], input_shape[1], 3)) batch_label = np.zeros(shape=(1, input_shape[0], input_shape[1], nbr_classes)) batch_disp_img = np.zeros(shape=(1, input_shape[0], input_shape[1], 1)) preprocessor_inst = InstanceToOneHot(class_order=list_classes) preprocessor_one_hot = OneHotEncoding(total_number_classes=nbr_classes) image = cv2.imread(os.path.join(input_path, img_name)) depth = cv2.imread(os.path.join(depth_path, img_name), 0) label = cv2.imread(os.path.join(output_path, label_name), 0) resized_image = cv2.resize(image, (input_shape[1], input_shape[0])) resized_depth = cv2.resize(depth, (input_shape[1], input_shape[0])) resized_label = cv2.resize(label, (input_shape[1], input_shape[0]), interpolation=cv2.INTER_NEAREST) index_based_label, _ = preprocessor_inst.pre_process(resized_label) one_hot_label, _ = preprocessor_one_hot.pre_process(index_based_label) # Predict disparity-map from pydnet # with tf.Graph().as_default(): # placeholders = {'im0': tf.placeholder(tf.float32, [None, None, None, 3], name='im0')} # # with tf.variable_scope("model") as scope: # model = pydnet(placeholders) # # init = tf.group(tf.global_variables_initializer(), # tf.local_variables_initializer()) # # loader = tf.train.Saver() # # with tf.Session() as sess: # sess.run(init) # loader.restore(sess, PYDNET_SAVED_WEIGHTS) # pyd_image = cv2.resize(image, (input_shape[1], input_shape[0])).astype(np.float32) # pyd_image = np.expand_dims(pyd_image, 0) # disp = sess.run(model.results[0], feed_dict={placeholders['im0']: pyd_image}) # left_disp = disp[0, :, :, 0] # #left_disp = np.expand_dims(left_disp, 3) batch_img[0] = resized_image batch_label[0] = one_hot_label batch_disp_img[0, :, :, 0] = resized_depth return batch_img, batch_label, batch_disp_img
def generate_input_output_pairs(input_path, output_path, batch_size, input_shape, list_classes, nbr_classes): # Read all the strings in the directory path batch_list = os.listdir(input_path) # Create indices for images if batch_size == "all": batch_size = len(batch_list) batch_indices = np.arange(0, batch_size) else: batch_indices = np.random.randint(low=0, high=len(batch_list), size=batch_size) # Preallocate matrices according to input sizes batch_img = np.zeros(shape=(batch_size, input_shape[0], input_shape[1], 3)) batch_label = np.zeros(shape=(batch_size, input_shape[0], input_shape[1], nbr_classes)) preprocessor_inst = InstanceToOneHot(class_order=list_classes) preprocessor_one_hot = OneHotEncoding(total_number_classes=nbr_classes) for i in range(batch_size): img_name = batch_list[batch_indices[i]] label_name = img_name.replace("leftImg8bit.jpg", "gtFine_labelIds.png") # label_name = batch_list[batch_indices[i]].split('.')[0] + '.jpg' image = cv2.imread(os.path.join(input_path, img_name)) label = cv2.imread(os.path.join(output_path, label_name), 0) resized_image = cv2.resize(image, (input_shape[1], input_shape[0])) resized_label = cv2.resize(label, (input_shape[1], input_shape[0]), interpolation=cv2.INTER_NEAREST) index_based_label, _ = preprocessor_inst.pre_process(resized_label) one_hot_label, _ = preprocessor_one_hot.pre_process(index_based_label) batch_img[i] = resized_image batch_label[i] = one_hot_label yield (batch_img, batch_label)
def generate_evaluation_batches(input_path, output_path, batch_size, input_shape, list_classes, nbr_classes): # create batch_name by taking random names, batch_list = os.listdir(input_path) if batch_size == "all": batch_size = len(batch_list) batch_indices = np.arange(0, batch_size) else: batch_indices = np.random.randint(low=0, high=len(batch_list), size=batch_size) # batch_img and batch_labels by reading corresponding images and labels. feed them into model batch_img = np.zeros(shape=(batch_size, input_shape[0], input_shape[1], 3)) batch_label = np.zeros(shape=(batch_size, input_shape[0], input_shape[1], nbr_classes)) preprocessor_inst = InstanceToOneHot(class_order=list_classes) preprocessor_one_hot = OneHotEncoding(total_number_classes=nbr_classes) for i in range(batch_size): img_name = batch_list[batch_indices[i]] #label_name = img_name.replace("leftImg8bit.jpg", "gtFine_labelIds.png") label_name = img_name.replace(".jpg", ".png") #label_name = batch_list[batch_indices[i]].split('.')[0] + '.jpg' image = cv2.imread(os.path.join(input_path, img_name)) label = cv2.imread(os.path.join(output_path, label_name), 0) resized_image = cv2.resize(image, (input_shape[1], input_shape[0])) resized_label = cv2.resize(label, (input_shape[1], input_shape[0]), interpolation=cv2.INTER_NEAREST) index_based_label, _ = preprocessor_inst.pre_process(resized_label) one_hot_label, _ = preprocessor_one_hot.pre_process(index_based_label) batch_img[i] = resized_image batch_label[i] = one_hot_label return batch_img, batch_label
def segnet_train_data_generator(input_path, output_path, batch_size, input_shape, list_classes, nbr_classes, dataset, task): # Create list of directory img_list = os.listdir(input_path) nbr_images = len(img_list) img_indices = list(range(len(img_list))) # Initialize the pre-process classes # if task == "train": # print("Using rgb list") #preprocessor_inst = InstanceToOneHot_rgb(class_order=list_classes) # else: preprocessor_inst = InstanceToOneHot(class_order=list_classes) preprocessor_one_hot = OneHotEncoding(total_number_classes=nbr_classes) # Load the ordering of the dataset index_path = "/MLDatasetsStorage/exjobb/" + dataset + "/RandomBatches.cvs" index_df = pd.read_csv(index_path) epoch = 1 counter = 0 index_list = index_df[str(epoch)].values while True: # Indexes for batch if task == "eval": batch_indices = random.sample(img_indices, batch_size) for delete_ind in batch_indices: img_indices.remove(delete_ind) if len(img_indices) < batch_size: img_indices = list(range(len(img_list))) elif len(img_indices) is None: img_indices = list(range(len(img_list))) #print("img_name eval:" + img_list[batch_indices]) else: if counter + 1 == nbr_images: print("Counter value: {} \n nbr_images: {}".format( counter + 1, nbr_images)) epoch = epoch + 1 counter = 0 index_list = index_df[str(epoch)].values print("Next epoch initialized") lim = counter + batch_size batch_indices = index_list[counter:lim] counter = counter + batch_size # initialize batch_img and batch_labels to the correct shape batch_img = np.zeros(shape=(batch_size, input_shape[0], input_shape[1], 3)) batch_label = np.zeros(shape=(batch_size, input_shape[0], input_shape[1], nbr_classes)) for i in range(batch_size): # Extract image name and generate it's corresponding label name img_name = img_list[batch_indices[i]] if dataset == "CityScapes": label_name = img_name.replace("leftImg8bit.jpg", "gtFine_labelIds.png") else: label_name = img_name.replace(".jpg", ".png") # Read image and label # if task == "train": #label_bgr = cv2.imread(os.path.join(output_path, label_name)) #label = cv2.cvtColor(label_bgr, cv2.COLOR_BGR2RGB) # else: label = cv2.imread(os.path.join(output_path, label_name), 0) image = cv2.imread(os.path.join(input_path, img_name)) # Resize image and label new_image = cv2.resize(image, (input_shape[1], input_shape[0])) new_label = cv2.resize(label, (input_shape[1], input_shape[0]), interpolation=cv2.INTER_NEAREST) # Call pre-processes new_label, _ = preprocessor_inst.pre_process(new_label) # print("Size of label shape before one_hot: {}".format(np.shape(new_label))) new_label, _ = preprocessor_one_hot.pre_process(new_label) # Add updated image and label to the return parameters batch_img[i] = new_image batch_label[i] = new_label yield batch_img, batch_label
def dsegnet_train_data_generator(input_path, output_path, depth_path, batch_size, input_shape, list_classes, nbr_classes, dataset, task): #PYDNET_SAVED_WEIGHTS = '/WeightModels/exjobb/OldStuff/pydnet_weights/pydnet_org/pydnet' # Create list of directory img_list = os.listdir(input_path) img_indices = list(range(len(img_list))) nbr_images = len(img_list) # Initialize the pre-process classes preprocessor_inst = InstanceToOneHot(class_order=list_classes) preprocessor_one_hot = OneHotEncoding(total_number_classes=nbr_classes) # Load the ordering of the dataset index_path = "/MLDatasetsStorage/exjobb/" + dataset + "/RandomBatches.cvs" index_df = pd.read_csv(index_path) epoch = 1 counter = 0 index_list = index_df[str(epoch)].values # Predict disparity-map from pydnet # with tf.Graph().as_default(): # placeholders = {'im0': tf.placeholder(tf.float32, [None, None, None, 3], name='im0')} # # with tf.variable_scope("model") as scope: # model = pydnet(placeholders) # # init = tf.group(tf.global_variables_initializer(), # tf.local_variables_initializer()) # # loader = tf.train.Saver() # Pre-process output labels for the batches while True: # Indexes for batch if task == "eval": batch_indices = random.sample(img_indices, batch_size) for delete_ind in batch_indices: img_indices.remove(delete_ind) if len(img_indices) < batch_size: img_indices = list(range(len(img_list))) elif len(img_indices) is None: img_indices = list(range(len(img_list))) #print("img_name eval:" + img_list[batch_indices]) else: if counter + 1 == nbr_images: print("Counter value: {} \n nbr_images: {}".format( counter + 1, nbr_images)) epoch = epoch + 1 counter = 0 index_list = index_df[str(epoch)].values print("Next epoch initialized") lim = counter + batch_size batch_indices = index_list[counter:lim] counter = counter + batch_size # initialize batch_img and batch_labels to the correct shape batch_img = np.zeros(shape=(batch_size, input_shape[0], input_shape[1], 3)) batch_label = np.zeros(shape=(batch_size, input_shape[0], input_shape[1], nbr_classes)) batch_disp_img = np.zeros(shape=(batch_size, input_shape[0], input_shape[1], 1)) for i in range(batch_size): # Extract image name and generate it's corresponding label name img_name = img_list[batch_indices[i]] label_name = img_name.replace(".jpg", ".png") # Read image and label label = cv2.imread(os.path.join(output_path, label_name), 0) image = cv2.imread(os.path.join(input_path, img_name)) left_disp = cv2.imread(os.path.join(depth_path, img_name), 0) # with tf.Session() as sess: # sess.run(init) # loader.restore(sess, PYDNET_SAVED_WEIGHTS) # pyd_image = cv2.resize(image, (input_shape[1], input_shape[0])).astype(np.float32) # pyd_image = np.expand_dims(pyd_image, 0) # disp = sess.run(model.results[0], feed_dict={placeholders['im0']: pyd_image}) # left_disp = disp[0, :, :, 0]*3.33 # left_disp = np.expand_dims(left_disp, 3) # Resize image and label new_image = cv2.resize(image, (input_shape[1], input_shape[0])) new_label = cv2.resize(label, (input_shape[1], input_shape[0]), interpolation=cv2.INTER_NEAREST) new_disp = cv2.resize(left_disp, (input_shape[1], input_shape[0]), interpolation=cv2.INTER_NEAREST) new_disp = np.expand_dims(new_disp, 2) # Call pre-processes # new_label = preprocessor_class.pre_process(new_label) new_label, _ = preprocessor_inst.pre_process(new_label) new_label, _ = preprocessor_one_hot.pre_process(new_label) # Add updated image and label to the return parameters batch_img[i] = new_image batch_label[i] = new_label batch_disp_img[i] = new_disp yield [batch_img, batch_disp_img], batch_label