def __getitem__(self, index): image = util.load_image(self.file_names[index]) label = util.load_label(self.file_names[index]) image, label = util.random_crop(image, label) image, label = util.random_augmentation(image, label) one_hot = util.one_hot(label, self.palette) return np.float32(image) / 255.0, np.float32(one_hot)
def main(): _, palette = util.get_label_info( os.path.join(config.data_dir, "class_dict.csv")) model = nn.build_model(classes=len(palette)) model(tf.zeros((1, config.height, config.width, 3))) file_names = [ file_name[:-4] for file_name in os.listdir( os.path.join(config.data_dir, config.image_dir)) ] for file_name in tqdm.tqdm(file_names): image = util.load_image(file_name) label = util.load_label(file_name) image, label = util.random_crop(image, label) image = np.expand_dims(image, 0).astype('float32') output = model.predict(image / 255.0) output = np.array(output[0, :, :, :]) output = np.argmax(output, axis=-1) output = util.colour_code_segmentation(output, palette) output = np.uint8(output) util.save_images([output, label], os.path.join('results', f'{file_name}.jpg'), titles=['Pred', 'Label'])
def __getitem__(self, index): image = util.load_image(self.file_names[index]) boxes, label = util.load_label(self.file_names[index]) image, boxes = util.resize(image, boxes) image, boxes = util.random_flip(image, boxes) image = image[:, :, ::-1].astype(numpy.float32) image = image / 255.0 y_true_1, y_true_2, y_true_3 = util.process_box(boxes, label) return image, y_true_1, y_true_2, y_true_3
def get_boxes(): boxes = [] file_names = [ file_name[:-4] for file_name in os.listdir( os.path.join(config.data_dir, config.label_dir)) ] for file_name in file_names: for box in util.load_label(file_name)[0]: boxes.append([box[2] - box[0], box[3] - box[1]]) return numpy.array(boxes)
def __getitem__(self, index): file_name = self.file_names[index] image = util.load_image(file_name) label = util.load_label(file_name) image, label = util.random_crop(image, label) image, label = util.random_augmentation(image, label) image = torch.from_numpy(image.transpose(2, 0, 1)) one_hot = util.one_hot_it(label, self.palette) one_hot = torch.from_numpy(one_hot) return image, one_hot
def __getitem__(self, index): image = load_image(self.file_names[index]) boxes, label = load_label(self.file_names[index]) boxes = np.concatenate( (boxes, np.full( shape=(boxes.shape[0], 1), fill_value=1., dtype=np.float32)), axis=-1) image, boxes = resize(image, boxes) image = image.astype(np.float32) image = image / 255.0 y_true_1, y_true_2, y_true_3 = process_box(boxes, label) return image, y_true_1, y_true_2, y_true_3
def get_boxes(): boxes = [] f_names = [ f_name[:-4] for f_name in os.listdir(join(config.data_dir, config.label_dir)) ] for f_name in f_names: annotations = load_label(f_name) for annotation in annotations[0]: x_min = annotation[0] y_min = annotation[1] x_max = annotation[2] y_max = annotation[3] boxes.append([x_max - x_min, y_max - y_min]) return np.array(boxes)
def write_tf_record(queue, sentinel): def byte_feature(value): if not isinstance(value, bytes): if not isinstance(value, list): value = value.encode('utf-8') else: value = [val.encode('utf-8') for val in value] if not isinstance(value, list): value = [value] return tf.train.Feature(bytes_list=tf.train.BytesList(value=value)) while True: file_name = queue.get() if file_name == sentinel: break in_image = util.load_image(file_name)[:, :, ::-1] boxes, label = util.load_label(file_name) in_image, boxes = util.resize(in_image, boxes) y_true_1, y_true_2, y_true_3 = util.process_box(boxes, label) in_image = in_image.astype('float32') y_true_1 = y_true_1.astype('float32') y_true_2 = y_true_2.astype('float32') y_true_3 = y_true_3.astype('float32') in_image = in_image.tobytes() y_true_1 = y_true_1.tobytes() y_true_2 = y_true_2.tobytes() y_true_3 = y_true_3.tobytes() features = tf.train.Features( feature={ 'in_image': byte_feature(in_image), 'y_true_1': byte_feature(y_true_1), 'y_true_2': byte_feature(y_true_2), 'y_true_3': byte_feature(y_true_3) }) tf_example = tf.train.Example(features=features) opt = tf.io.TFRecordOptions('GZIP') with tf.io.TFRecordWriter( os.path.join(config.data_dir, 'TF', file_name + ".tf"), opt) as writer: writer.write(tf_example.SerializeToString())
def get_boxes(): boxes = [] f_names = [ f_name[:-4] for f_name in os.listdir(join(config.base_dir, config.label_dir)) ] for f_name in f_names: annotations = load_label(f_name) for annotation in annotations: x_min = annotation[0] y_min = annotation[1] x_max = annotation[2] y_max = annotation[3] width = x_max - x_min height = y_max - y_min boxes.append([width, height]) return np.array(boxes)
def __getitem__(self, index): image = load_image(self.f_names[index]) boxes, label = load_label(self.f_names[index]) boxes = np.concatenate( (boxes, np.full( shape=(boxes.shape[0], 1), fill_value=1., dtype=np.float32)), axis=-1) image, boxes = random_horizontal_flip(image, boxes) image, boxes = random_crop(image, boxes) image, boxes = random_translate(image, boxes) image, boxes = resize(image, boxes) image = image.astype(np.float32) image /= 255. mean = [0.485, 0.456, 0.406] std = [0.229, 0.224, 0.225] image -= mean image /= std y_true_1, y_true_2, y_true_3 = process_box(boxes, label) return image, y_true_1, y_true_2, y_true_3
def evaluate(): model = torch.load(os.path.join('weights', 'model.pt'), device)['model'].float().eval() half = device.type != 'cpu' if half: model.half() model.eval() img = torch.zeros((1, 3, config.image_size, config.image_size), device=device) _ = model(img.half() if half else img) if device.type != 'cpu' else None file_names = [ file_name[:-4] for file_name in os.listdir( os.path.join(config.data_dir, config.image_dir)) ] for file_name in file_names: image = util.load_image(file_name) label = util.load_label(file_name) image, label = util.resize(image, label) image = torch.from_numpy(np.expand_dims(image.transpose(2, 0, 1), 0)) image = image.to(device, non_blocking=True) image = image.half() if half else image.float() pred = model(image / 255.0) pred = np.array(pred.cpu().detach().numpy()[0, :, :, :].transpose( 1, 2, 0)) pred = util.reverse_one_hot(pred) pred = util.colour_code_segmentation(pred, palette) pred = np.uint8(pred) util.save_images([label, pred], ['TRUE', 'PRED'], os.path.join('results', f'{file_name}.jpg'))