def predict(self): print('---->predicting ', self.conf.test_epoch) if self.conf.test_epoch > 0: self.reload(self.conf.test_epoch) else: print("please set a reasonable test_epoch") return test_reader = H5DataLoader( self.conf.data_dir+self.conf.test_data, False) self.sess.run(tf.local_variables_initializer()) predictions = [] losses = [] accuracies = [] m_ious = [] count=0 while True: inputs, annotations = test_reader.next_batch(self.conf.batch) if inputs.shape[0] < self.conf.batch: break feed_dict = {self.inputs: inputs, self.annotations: annotations} loss, accuracy, m_iou, _ = self.sess.run( [self.loss_op, self.accuracy_op, self.m_iou, self.miou_op], feed_dict=feed_dict) print('values----->', loss, accuracy, m_iou) losses.append(loss) accuracies.append(accuracy) m_ious.append(m_iou) predictions.append(self.sess.run( self.decoded_predictions, feed_dict=feed_dict)) print('----->saving predictions') for index, prediction in enumerate(predictions): for i in range(prediction.shape[0]): imsave(prediction[i], self.conf.sample_dir + str(index*prediction.shape[0]+i)+'.png') return np.mean(losses),np.mean(accuracies),m_ious[-1]
def predict(self): print('---->predicting ', self.conf.test_epoch) if self.conf.test_epoch > 0: self.reload(self.conf.test_epoch) else: print("please set a reasonable test_epoch") return # 读取数据 test_reader = H5DataLoader(self.conf.data_dir + self.conf.test_data, False) self.sess.run(tf.local_variables_initializer()) predictions = [] losses = [] accuracies = [] m_ious = [] while True: inputs, annotations = test_reader.next_batch(self.conf.batch) # 终止条件 if inputs.shape[0] < self.conf.batch: break feed_dict = {self.inputs: inputs, self.annotations: annotations} loss, accuracy, m_iou, _ = self.sess.run( [self.loss_op, self.accuracy_op, self.m_iou, self.miou_op], feed_dict=feed_dict) print('values----->', loss, accuracy, m_iou) # 记录指标 losses.append(loss) accuracies.append(accuracy) m_ious.append(m_iou) # 记录预测值 predictions.append( self.sess.run(self.decoded_predictions, feed_dict=feed_dict)) print('----->saving predictions') print(np.shape(predictions)) num = 0 for index, prediction in enumerate(predictions): # 下面的程序用于输出一通道的预测值,测试时需要观察的 #print(prediction.shape) #print(index) #np.save("pred",np.array(prediction)) # 把一通道的预测值保存为三通道图片,这是自己写的函数 for i in range(prediction.shape[0]): np.save(self.conf.sample_dir + "pred" + str(num) + ".npy", prediction[i]) num += 1 imsave( prediction[i], self.conf.sample_dir + str(index * prediction.shape[0] + i) + '.png') # 验证和测试的时候,指标都是返回的全体上的均值 return np.mean(losses), np.mean(accuracies), m_ious[-1]
def save_labelme_datav1(file_path, image_path, image_data, annotations_list, label_to_text=lambda x: str(x)): wmli.imsave(image_path, image_data) image = {"width": image_data.shape[1], "height": image_data.shape[0]} save_labelme_data(file_path, image_path, image, annotations_list, label_to_text)
def predict(self): self.reload(self.conf.test_epoch) test_reader = H5DataLoader(self.conf.data_dir + self.conf.test_data, False) self.sess.run(tf.local_variables_initializer()) predictions = [] net_predictions = [] losses = [] dices = [] accuracies = [] count = 0 while True: inputs, annotations = test_reader.next_batch(self.conf.batchsize) if inputs.shape[0] < self.conf.batch: break feed_dict = { self.inputs: inputs, self.annotations: annotations, self.is_train: False } predictions.append( self.sess.run( self.predictions, feed_dict=feed_dict)) # <-------decoded_predictions net_predictions.append( self.sess.run(self.decoded_net_pred, feed_dict=feed_dict)) count += 1 if count == self.conf.test_num: break num = 0 for index, prediction in enumerate(net_predictions): for i in range(prediction.shape[0]): num += 1 imsave( prediction[i], self.conf.sample_net_dir + str(index * prediction.shape[0] + i) + '.png') return np.mean(losses), np.mean(accuracies), np.mean(dices)
def predict(self): print('---->predicting ', self.conf.test_epoch) if self.conf.test_epoch > 0: self.reload(self.conf.test_epoch) else: print("please set a reasonable test_epoch") return test_reader = H5DataLoader( self.conf.data_dir+self.conf.test_data, False) predictions = [] while True: inputs, annotations = test_reader.next_batch(self.conf.batch) if inputs.shape[0] < self.conf.batch: break feed_dict = {self.inputs: inputs, self.annotations: annotations} predictions.append(self.sess.run( self.decoded_predictions, feed_dict=feed_dict)) print('----->saving predictions') for index, prediction in enumerate(predictions): for i in range(prediction.shape[0]): imsave(prediction[i], self.conf.sample_dir + str(index*prediction.shape[0]+i)+'.png')
def _evaluate_denoise(sr_model : BaseSuperResolutionModel, validation_dir, scale_pred=False): print("Validating %s model" % sr_model.model_name) predict_path = "val_predict/" if not os.path.exists(predict_path): os.makedirs(predict_path) validation_path_set5 = validation_dir + "set5/" validation_path_set14 = validation_dir + "set14/" validation_dirs = [validation_path_set5, validation_path_set14] for val_dir in validation_dirs: image_fns = [name for name in os.listdir(val_dir)] nb_images = len(image_fns) print("Validating %d images from path %s" % (nb_images, val_dir)) total_psnr = 0.0 for impath in os.listdir(val_dir): t1 = time.time() # Input image y = img_utils.imread(val_dir + impath, mode='RGB') width, height, _ = y.shape if ((width // sr_model.scale_factor) % 4 != 0) or ((height // sr_model.scale_factor) % 4 != 0) \ or (width % 2 != 0) or (height % 2 != 0): width = ((width // sr_model.scale_factor) // 4) * 4 * sr_model.scale_factor height = ((height // sr_model.scale_factor) // 4) * 4 * sr_model.scale_factor print("Model %s require the image size to be divisible by 4. New image size = (%d, %d)" % \ (sr_model.model_name, width, height)) y = img_utils.imresize(y, (width, height), interp='bicubic') y = y.astype('float32') y = np.expand_dims(y, axis=0) x_temp = y.copy() if sr_model.type_scale_type == "tanh": x_temp = (x_temp - 127.5) / 127.5 y = (y - 127.5) / 127.5 else: x_temp /= 255. y /= 255. img = img_utils.imresize(x_temp[0], (width // sr_model.scale_factor, height // sr_model.scale_factor), interp='bicubic', mode='RGB') if not sr_model.type_true_upscaling: img = img_utils.imresize(img, (width, height), interp='bicubic') x = np.expand_dims(img, axis=0) if K.image_dim_ordering() == "th": x = x.transpose((0, 3, 1, 2)) y = y.transpose((0, 3, 1, 2)) sr_model.model = sr_model.create_model(height, width, load_weights=True) if sr_model.evaluation_func is None: if sr_model.uses_learning_phase: sr_model.evaluation_func = K.function([sr_model.model.layers[0].input, K.learning_phase()], [sr_model.model.layers[-1].output]) else: sr_model.evaluation_func = K.function([sr_model.model.layers[0].input], [sr_model.model.layers[-1].output]) if sr_model.uses_learning_phase: y_pred = sr_model.evaluation_func([x, 0])[0][0] else: y_pred = sr_model.evaluation_func([x])[0][0] if scale_pred: if sr_model.type_scale_type == "tanh": y_pred = (y_pred + 1) * 127.5 else: y_pred *= 255. if sr_model.type_scale_type == 'tanh': y = (y + 1) / 2 psnr_val = psnr(y[0], np.clip(y_pred, 0, 255) / 255) total_psnr += psnr_val t2 = time.time() print("Validated image : %s, Time required : %0.2f, PSNR value : %0.4f" % (impath, t2 - t1, psnr_val)) generated_path = predict_path + "%s_%s_generated.png" % (sr_model.model_name, os.path.splitext(impath)[0]) if K.image_dim_ordering() == "th": y_pred = y_pred.transpose((1, 2, 0)) y_pred = np.clip(y_pred, 0, 255).astype('uint8') img_utils.imsave(generated_path, y_pred) print("Average PRNS value of validation images = %00.4f \n" % (total_psnr / nb_images))
def evaluate(self, validation_dir, scale_factor, target_size=256, small_train_images=False): """ Evaluates the model on the Set5 Validation images """ if self.model == None: self.create_model(load_weights=True, small_train_images=small_train_images) if self.evaluation_func is None: self.evaluation_func = K.function([self.model.layers[0].input], [self.model.layers[-1].output]) predict_path = "val_predict/" if not os.path.exists(predict_path): os.makedirs(predict_path) image_fns = [name for name in os.listdir(validation_dir)] nb_images = len(image_fns) print("Validating %d images" % (nb_images)) total_psnr = 0.0 for impath in os.listdir(validation_dir): t1 = time.time() # Input image y = img_utils.imread(validation_dir + impath, mode='RGB') width, height, _ = y.shape if self.model_name in self.denoise_models: # Denoise models require precise width and height, divisible by 4 if ((width // scale_factor) % 4 != 0) or ( (height // scale_factor) % 4 != 0): width = ((width // scale_factor) // 4) * 4 * scale_factor height = ((height // scale_factor) // 4) * 4 * scale_factor print("Model %s require the image size to be divisible by 4. New image size = (%d, %d)" % \ (self.model_name, width, height)) y = img_utils.imresize(y, (width, height), interp='bicubic') y = y.astype('float32') / 255. y = np.expand_dims(y, axis=0) x_width = width if not small_train_images else width // scale_factor x_height = height if not small_train_images else height // scale_factor x_temp = y.copy() img = img_utils.gaussian_filter(x_temp[0], sigma=0.01) img = img_utils.imresize( img, (x_width // scale_factor, x_height // scale_factor), interp='bicubic') if not small_train_images: img = img_utils.imresize(img, (x_width, x_height), interp='bicubic') x = np.expand_dims(img, axis=0) if K.image_dim_ordering() == "th": x = x.transpose((0, 3, 1, 2)) y = y.transpose((0, 3, 1, 2)) y_pred = self.evaluation_func([x])[0][0] psnr_val = psnr(y[0], np.clip(y_pred, 0, 255) / 255) total_psnr += psnr_val t2 = time.time() print( "Validated image : %s, Time required : %0.2f, PSNR value : %0.4f" % (impath, t2 - t1, psnr_val)) generated_path = predict_path + "%s_generated.png" % ( os.path.splitext(impath)[0]) if K.image_dim_ordering() == "th": y_pred = y_pred.transpose((1, 2, 0)) y_pred = np.clip(y_pred, 0, 255).astype('uint8') img_utils.imsave(generated_path, y_pred) print("Average PRNS value of validation images = %00.4f" % (total_psnr / nb_images))
help="path to test data dir") parser.add_argument("--save_dir", default="/2_data/wj/mldata/coco/coco_results", type=str, help="path to save data dir") return parser if __name__ == "__main__": args = default_argument_parser().parse_args() save_path = args.save_dir dataset = PascalVOCData() # data_path0 = get_data_dir2("annotationed_data/verify_p03_1020_1_proc_m") data_path = args.test_dir if not dataset.read_data(data_path): exit(0) wmlu.create_empty_dir(save_path, remove_if_exists=True, yes_to_all=True) for data in dataset.get_items(): full_path, shape, category_ids, category_names, boxes, binary_masks, area, is_crowd, num_annotations_skipped = data print(f"Process {full_path}") img = wmli.imread(full_path) base_name = wmlu.base_name(full_path) + "_a.jpg" save_file_path = os.path.join(save_path, base_name) img = odv.draw_bboxes(img, classes=category_names, bboxes=boxes, show_text=True, text_color=(255., 0., 0.)) wmli.imsave(save_file_path, img)
def evaluate(self, validation_dir, scale_factor, target_size=256, small_train_images=False): """ Evaluates the model on the Set5 Validation images """ if self.model == None: self.create_model(load_weights=True, small_train_images=small_train_images) if self.evaluation_func is None: self.evaluation_func = K.function([self.model.layers[0].input], [self.model.layers[-1].output]) predict_path = "val_predict/" if not os.path.exists(predict_path): os.makedirs(predict_path) image_fns = [name for name in os.listdir(validation_dir)] nb_images = len(image_fns) print("Validating %d images" % (nb_images)) total_psnr = 0.0 for impath in os.listdir(validation_dir): t1 = time.time() # Input image y = img_utils.imread(validation_dir + impath, mode='RGB') width, height, _ = y.shape if self.model_name in self.denoise_models: # Denoise models require precise width and height, divisible by 4 if ((width // scale_factor) % 4 != 0) or ((height // scale_factor) % 4 != 0): width = ((width // scale_factor) // 4) * 4 * scale_factor height = ((height // scale_factor) // 4) * 4 * scale_factor print("Model %s require the image size to be divisible by 4. New image size = (%d, %d)" % \ (self.model_name, width, height)) y = img_utils.imresize(y, (width, height), interp='bicubic') y = y.astype('float32') / 255. y = np.expand_dims(y, axis=0) x_width = width if not small_train_images else width // scale_factor x_height = height if not small_train_images else height // scale_factor x_temp = y.copy() img = img_utils.gaussian_filter(x_temp[0], sigma=0.01) img = img_utils.imresize(img, (x_width // scale_factor, x_height // scale_factor), interp='bicubic') if not small_train_images: img = img_utils.imresize(img, (x_width, x_height), interp='bicubic') x = np.expand_dims(img, axis=0) if K.image_dim_ordering() == "th": x = x.transpose((0, 3, 1, 2)) y = y.transpose((0, 3, 1, 2)) y_pred = self.evaluation_func([x])[0][0] psnr_val = psnr(y[0], np.clip(y_pred, 0, 255) / 255) total_psnr += psnr_val t2 = time.time() print("Validated image : %s, Time required : %0.2f, PSNR value : %0.4f" % (impath, t2 - t1, psnr_val)) generated_path = predict_path + "%s_generated.png" % (os.path.splitext(impath)[0]) if K.image_dim_ordering() == "th": y_pred = y_pred.transpose((1, 2, 0)) y_pred = np.clip(y_pred, 0, 255).astype('uint8') img_utils.imsave(generated_path, y_pred) print("Average PRNS value of validation images = %00.4f" % (total_psnr / nb_images))
def predict(self): print('---->predicting ', self.conf.test_epoch) if self.conf.test_epoch > 0: self.reload(self.conf.test_epoch) else: print("please set a reasonable test_epoch") return test_reader = H5DataLoader(self.conf.data_dir + self.conf.test_data, False) self.sess.run(tf.local_variables_initializer()) predictions = [] net_predictions = [] outputs = [] probabilitys = [] losses = [] accuracies = [] m_ious = [] rate_list = [] befores = [] afters = [] maps = [] start_maps = [] count = 0 while True: inputs, annotations = test_reader.next_batch(self.conf.batchsize) if inputs.shape[0] < self.conf.batch: break feed_dict = { self.inputs: inputs, self.annotations: annotations, self.is_train: False } loss, accuracy, m_iou, _ = self.sess.run( [self.loss_op, self.accuracy_op, self.m_iou, self.miou_op], feed_dict=feed_dict) print('values----->', loss, accuracy, m_iou) losses.append(loss) accuracies.append(accuracy) m_ious.append(m_iou) predictions.append( self.sess.run(self.decoded_predictions, feed_dict=feed_dict)) net_predictions.append( self.sess.run(self.decoded_net_pred, feed_dict=feed_dict)) outputs.append(self.sess.run(self.outputs, feed_dict=feed_dict)) count += 1 if count == self.conf.test_num: break print('----->saving outputs') print(np.shape(probabilitys)) np.save(self.conf.sample_dir + "outputs" + ".npy", np.array(outputs)) print('----->saving predictions') print(np.shape(predictions)) num = 0 for index, prediction in enumerate(predictions): for i in range(prediction.shape[0]): np.save(self.conf.sample_dir + "pred" + str(num) + ".npy", prediction[i]) num += 1 imsave( prediction[i], self.conf.sample_dir + str(index * prediction.shape[0] + i) + '.png') print('----->saving net_predictions') print(np.shape(net_predictions)) num = 0 for index, prediction in enumerate(net_predictions): for i in range(prediction.shape[0]): np.save(self.conf.sample_dir + "netpred" + str(num) + ".npy", prediction[i]) num += 1 imsave( prediction[i], self.conf.sample_dir + str(index * prediction.shape[0] + i) + 'net.png') return np.mean(losses), np.mean(accuracies), m_ious[-1]
def evaluate(self, validation_dir, small_train_images=False): print("Validating %s model" % self.model_name) predict_path = "val_predict/" if not os.path.exists(predict_path): os.makedirs(predict_path) validation_path_set5 = validation_dir + "set5/" validation_path_set14 = validation_dir + "set14/" validation_dirs = [validation_path_set5, validation_path_set14] for val_dir in validation_dirs: image_fns = [name for name in os.listdir(val_dir)] nb_images = len(image_fns) print("Validating %d images from path %s" % (nb_images, val_dir)) total_psnr = 0.0 for impath in os.listdir(val_dir): t1 = time.time() # Input image y = img_utils.imread(val_dir + impath, mode='RGB') width, height, _ = y.shape y = y.astype('float32') / 255. y = np.expand_dims(y, axis=0) x_temp = y.copy() img = img_utils.imresize( x_temp[0], (width // self.scale_factor, height // self.scale_factor), interp='bicubic') if not small_train_images: img = img_utils.imresize(img, (width, height), interp='bicubic') x = np.expand_dims(img, axis=0) if K.image_dim_ordering() == "th": x = x.transpose((0, 3, 1, 2)) y = y.transpose((0, 3, 1, 2)) self.model = self.create_model(height, width, load_weights=True) self.evaluation_func = K.function( [self.model.layers[0].input], [self.model.layers[-1].output]) y_pred = self.evaluation_func([x])[0][0] psnr_val = psnr(y[0], np.clip(y_pred, 0, 255) / 255) total_psnr += psnr_val t2 = time.time() print( "Validated image : %s, Time required : %0.2f, PSNR value : %0.4f" % (impath, t2 - t1, psnr_val)) generated_path = predict_path + "%s_%s_generated.png" % ( self.model_name, os.path.splitext(impath)[0]) if K.image_dim_ordering() == "th": y_pred = y_pred.transpose((1, 2, 0)) y_pred = np.clip(y_pred, 0, 255).astype('uint8') img_utils.imsave(generated_path, y_pred) print("Average PRNS value of validation images = %00.4f \n" % (total_psnr / nb_images))