def build_ssd7_wider_face(weights_path=WEIGHTS_PATH): # 0. Clear the previous models from memory K.clear_session() # 1. Build the model model = build_model(image_size=(img_height, img_width, img_channels), n_classes=n_classes, mode='training', l2_regularization=0.0005, scales=scales, aspect_ratios_global=aspect_ratios, aspect_ratios_per_layer=None, two_boxes_for_ar1=two_boxes_for_ar1, steps=steps, offsets=offsets, clip_boxes=clip_boxes, variances=variances, normalize_coords=normalize_coords, subtract_mean=intensity_mean, divide_by_stddev=intensity_range) # 2. Load weights in case of pre-trained if weights_path is not None: model.load_weights(weights_path) # 3. Compile the model adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0) model.compile(optimizer=adam, loss=ssd_loss.compute_loss) #4. Return the model return model
def build_ssd7_model(self): intensity_mean = 127.5 # 像素减去 intensity_range = 127.5 # 像素除以 K.clear_session() # Clear previous models from memory. model = build_model(image_size=(self.img_height, self.img_width, self.img_channels), n_classes=self.n_classes, mode='training', l2_regularization=0.0005, scales=self.scales, aspect_ratios_global=self.aspect_ratios, aspect_ratios_per_layer=None, variances=self.variances, normalize_coords=self.normalize_coords, subtract_mean=intensity_mean, divide_by_stddev=intensity_range) # model.load_weights('./ssd7_weights.h5', by_name=True) # 迁移学习,微调参数 adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0) model.compile(optimizer=adam, loss=ssd_loss.compute_loss) return model
def __init__(self, listener): # label of traffic sign detected by this traffic sign detector self.sign = -1 # image lister object which can provide current frame self.listener = listener # build model self.model = build_model(image_size=(img_height, img_width, img_channels), n_classes=n_classes, mode='training', l2_regularization=0.0005, scales=scales, aspect_ratios_global=aspect_ratios, aspect_ratios_per_layer=None, two_boxes_for_ar1=two_boxes_for_ar1, steps=steps, offsets=offsets, clip_boxes=clip_boxes, variances=variances, normalize_coords=normalize_coords, subtract_mean=intensity_mean, divide_by_stddev=intensity_range) # 2: Optional: Load some weights self.model.load_weights( 'models/ssd7_epoch-04_loss-0.2610_val_loss-0.6570.h5', by_name=True) # 3: Instantiate an Adam optimizer and the SSD loss function and compile the model adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0) self.model.compile(optimizer=adam, loss=ssd_loss.compute_loss) img = cv2.imread( "/home/namntse05438/Cuoc_Dua_So/Ros_python/traffic_sign_data/1545920226.49_50_1.54542034912e-05.jpg" ) img = self.preprocessing(img) rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) y_pred_decoded = self.get_ypred_decoded( rgb_img.reshape(1, img_height, img_width, 3)) print(y_pred_decoded[0]) for box in y_pred_decoded[0]: xmin = int(box[-4]) ymin = int(box[-3]) xmax = int(box[-2]) ymax = int(box[-1]) label = '{}: {:.2f}'.format(classes[int(box[0])], box[1]) print(label) #draw bounding box cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 0, 255), 2) cv2.putText(img, str(label), (xmin, ymin + 20), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0), 2, cv2.LINE_AA) # cv2.imshow('traffic_sign_test', img) cv2.waitKey(1)
def __init__(self): K.clear_session() # Clear previous models from memory. self.nga_tu = 0 self.flag = 0 self.steering_bool = False self.count_for_traffic = 0 self.count = 0 self.pre_count = 0 self.old_count = 0 self.flag_car = False self.position_count_left = 0 self.position_count_right = 0 self.vi_tri_vat_can = 0 self.vi_tri_vat_can_hough = 0 config = tf.ConfigProto() config.gpu_options.allow_growth = True config.intra_op_parallelism_threads = 4 config.inter_op_parallelism_threads = 4 self.session = tf.InteractiveSession(config=config) self.graph = tf.get_default_graph() self.model = G2_Unet() self.model.summary() model_path = rospy.get_param('~model_dl') self.model.load_weights(model_path) self.subscriber1 = rospy.Subscriber( "g2_never_die/camera/rgb/compressed", CompressedImage, self.callback, queue_size=10) self.speed = rospy.Publisher("g2_never_die/set_speed", Float32, queue_size=1) self.angle_car = rospy.Publisher("g2_never_die/set_angle", Float32, queue_size=1) # self.traffic_sign = rospy.Subscriber("g2_never_die/traffic_sign", # Int8, # self.callback_sign, # queue_size=1) # self.depth_x = rospy.Subscriber("g2_never_die/depth_x",Float32,self.depth_x_callback,queue_size=1) # self.depth_y = rospy.Subscriber("g2_never_die/depth_y",Float32,self.depth_y_callback,queue_size=1) self.flag_sign = 0 self.balance_road = 0 self.den_diem_re = False self.stop_here = False self.depth_x_coor = 0 self.depth_y_coor = 0 self.check_point = 0 self.count_for_car = 0 self.check_car = False self.last_angle = 0 self.check_balance = False self.flag_for_re = False self.nga_tu_real = 0 self.check_nga_tu = False self.nga_tu_fake = 0 self.dem_nga_tu = 0 self.count_for_bungbinh = 0 self.count_for_car_real = 0 self.flag_car_for_real = False self.count_for_sign = 0 self.drive_for_my_way = False self.da_den_bung_binh = False self.cX_center = 0 self.cY_center = 0 ## SSD ## img_height = 240 # Height of the input images img_width = 320 # Width of the input images img_channels = 3 # Number of color channels of the input images intensity_mean = 127.5 # Set this to your preference (maybe `None`). The current settings transform the input pixel values to the interval `[-1,1]`. intensity_range = 127.5 # Set this to your preference (maybe `None`). The current settings transform the input pixel values to the interval `[-1,1]`. n_classes = 3 # Number of positive classes scales = [ 0.08, 0.16, 0.32, 0.64, 0.96 ] # An explicit list of anchor box scaling factors. If this is passed, it will override `min_scale` and `max_scale`. aspect_ratios = [0.5, 1.0, 2.0] # The list of aspect ratios for the anchor boxes two_boxes_for_ar1 = True # Whether or not you want to generate two anchor boxes for aspect ratio 1 steps = None # In case you'd like to set the step sizes for the anchor box grids manually; not recommended offsets = None # In case you'd like to set the offsets for the anchor box grids manually; not recommended clip_boxes = False # Whether or not to clip the anchor boxes to lie entirely within the image boundaries variances = [ 1.0, 1.0, 1.0, 1.0 ] # The list of variances by which the encoded target coordinates are scaled self.normalize_coords = True # Whether or not the model is supposed to use coordinates relative to the image size # weight_path = 'trained_models/digit_detect_pretrained.h5' weight_path = rospy.get_param('~ssd_model') self.ssd_model = build_model(image_size=(img_height, img_width, img_channels), n_classes=n_classes, mode='training', l2_regularization=0.0005, scales=scales, aspect_ratios_global=aspect_ratios, aspect_ratios_per_layer=None, two_boxes_for_ar1=two_boxes_for_ar1, steps=steps, offsets=offsets, clip_boxes=clip_boxes, variances=variances, normalize_coords=self.normalize_coords, subtract_mean=intensity_mean, divide_by_stddev=intensity_range) if (weight_path is not None): self.ssd_model.load_weights(weight_path, by_name=True) adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0) self.ssd_model.compile(optimizer=adam, loss=ssd_loss.compute_loss)
clip_boxes = False # The list/tuple of variances by which the encoded target coordinates are scaled variances = [1.0, 1.0, 1.0, 1.0] # Whether or not the model is supposed to use coordinates relative to the image size normalize_coords = True # 1: Build the Keras model K.clear_session() # Clear previous models from memory. model = build_model(image_size=(img_height, img_width, img_channels), n_classes=n_classes, mode='training', l2_regularization=0.0005, scales=scales, aspect_ratios_global=aspect_ratios, aspect_ratios_per_layer=None, two_boxes_for_ar1=two_boxes_for_ar1, steps=steps, offsets=offsets, clip_boxes=clip_boxes, variances=variances, normalize_coords=normalize_coords, subtract_mean=intensity_mean, divide_by_stddev=intensity_range) model.summary() # 2: Optional: Load some weights # model.load_weights('./ssd7_weights.h5', by_name=True) # 3: Instantiate an Adam optimizer and the SSD loss function and compile the model adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
def __init__(self, on_sim): ## to force CPU mode # import os # os.environ['CUDA_VISIBLE_DEVICES'] = '-1' from keras.backend.tensorflow_backend import set_session config = tf.ConfigProto() config.gpu_options.allow_growth = True set_session(tf.Session(config=config)) self.img_height = 600 # 396 #300 # Height of the input images self.img_width = 800 # 634 #480 # Width of the input images self.img_channels = 3 # Number of color channels of the input images intensity_mean = 127.5 # Set this to your preference (maybe `None`). The current settings transform the input pixel values to the interval `[-1,1]`. intensity_range = 127.5 # Set this to your preference (maybe `None`). The current settings transform the input pixel values to the interval `[-1,1]`. n_classes = 3 # 4 # Number of positive classes scales = [ 0.08, 0.16, 0.32, 0.64, 0.96 ] # An explicit list of anchor box scaling factors. If this is passed, it will override `min_scale` and `max_scale`. aspect_ratios = [0.5, 1.0, 2.0] # The list of aspect ratios for the anchor boxes two_boxes_for_ar1 = True # Whether or not you want to generate two anchor boxes for aspect ratio 1 steps = None # In case you'd like to set the step sizes for the anchor box grids manually; not recommended offsets = None # In case you'd like to set the offsets for the anchor box grids manually; not recommended clip_boxes = False # Whether or not to clip the anchor boxes to lie entirely within the image boundaries variances = [ 1.0, 1.0, 1.0, 1.0 ] # The list of variances by which the encoded target coordinates are scaled self.normalize_coords = True # Whether or not the model is supposed to use coordinates relative to the image size K.clear_session() # Clear previous self.model = build_model(image_size=(self.img_height, self.img_width, self.img_channels), n_classes=n_classes, mode='training', l2_regularization=0.0005, scales=scales, aspect_ratios_global=aspect_ratios, aspect_ratios_per_layer=None, two_boxes_for_ar1=two_boxes_for_ar1, steps=steps, offsets=offsets, clip_boxes=clip_boxes, variances=variances, normalize_coords=self.normalize_coords, subtract_mean=intensity_mean, divide_by_stddev=intensity_range) weights_path = 'ssd7_epoch-14_loss-1.0911_val_loss-0.5348.h5' if not on_sim: #TODO update this weights_path = 'ssd7_epoch-14_loss-1.0911_val_loss-0.5348.h5' self.model.load_weights(weights_path, by_name=True) global graph graph = tf.get_default_graph() adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0) self.model.compile(optimizer=adam, loss=ssd_loss.compute_loss) pass
def main(): create_new_model = True if args.model_name == 'default' else False if create_new_model: K.clear_session() # Clear previous models from memory. model = build_model(image_size=(Config.img_height, Config.img_width, Config.img_channels), n_classes=Config.n_classes, mode='training', l2_regularization=Config.l2_regularization, scales=Config.scales, aspect_ratios_global=Config.aspect_ratios, aspect_ratios_per_layer=None, two_boxes_for_ar1=Config.two_boxes_for_ar1, steps=Config.steps, offsets=Config.offsets, clip_boxes=Config.clip_boxes, variances=Config.variances, normalize_coords=Config.normalize_coords, subtract_mean=Config.intensity_mean, divide_by_stddev=Config.intensity_range) # model.load_weights("./weights/"+ args.model_name + ".h5", by_name=True) adam = Adam(lr=args.learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0) model.compile(optimizer=adam, loss=ssd_loss.compute_loss) else: model_path = "weights/" + args.model_name + ".h5" # We need to create an SSDLoss object in order to pass that to the model loader. ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0) K.clear_session() # Clear previous models from memory. model = load_model(model_path, custom_objects={ 'AnchorBoxes': AnchorBoxes, 'compute_loss': ssd_loss.compute_loss }) # Load the data train_dataset = DataGenerator(load_images_into_memory=True, hdf5_dataset_path=os.getcwd() + "/data/" + args.dataset + '/polyp_train.h5') val_dataset = DataGenerator(load_images_into_memory=True, hdf5_dataset_path=os.getcwd() + "/data/" + args.dataset + '/polyp_val.h5') train_dataset_size = train_dataset.get_dataset_size() val_dataset_size = val_dataset.get_dataset_size() print("Number of images in the training dataset:\t{:>6}".format( train_dataset_size)) print("Number of images in the validation dataset:\t{:>6}".format( val_dataset_size)) batch_size = args.batch_size # 4: Define the image processing chain. data_augmentation_chain = DataAugmentationConstantInputSize( random_brightness=(-48, 48, 0.5), random_contrast=(0.5, 1.8, 0.5), random_saturation=(0.5, 1.8, 0.5), random_hue=(18, 0.5), random_flip=0.5, random_translate=((0.03, 0.5), (0.03, 0.5), 0.5), random_scale=(0.5, 2.0, 0.5), n_trials_max=3, clip_boxes=True, overlap_criterion='area', bounds_box_filter=(0.3, 1.0), bounds_validator=(0.5, 1.0), n_boxes_min=1, background=(0, 0, 0)) # 5: Instantiate an encoder that can encode ground truth labels into the format needed by the SSD loss function. # The encoder constructor needs the spatial dimensions of the model's predictor layers to create the anchor boxes. predictor_sizes = [ model.get_layer('classes4').output_shape[1:3], model.get_layer('classes5').output_shape[1:3], model.get_layer('classes6').output_shape[1:3], model.get_layer('classes7').output_shape[1:3] ] ssd_input_encoder = SSDInputEncoder( img_height=Config.img_height, img_width=Config.img_width, n_classes=Config.n_classes, predictor_sizes=predictor_sizes, scales=Config.scales, aspect_ratios_global=Config.aspect_ratios, two_boxes_for_ar1=Config.two_boxes_for_ar1, steps=Config.steps, offsets=Config.offsets, clip_boxes=Config.clip_boxes, variances=Config.variances, matching_type='multi', pos_iou_threshold=0.5, neg_iou_limit=0.3, normalize_coords=Config.normalize_coords) # 6: Create the generator handles that will be passed to Keras' `fit_generator()` function. train_generator = train_dataset.generate( batch_size=batch_size, shuffle=True, transformations=[data_augmentation_chain], label_encoder=ssd_input_encoder, returns={'processed_images', 'encoded_labels'}, keep_images_without_gt=False) val_generator = val_dataset.generate( batch_size=batch_size, shuffle=False, transformations=[], label_encoder=ssd_input_encoder, returns={'processed_images', 'encoded_labels'}, keep_images_without_gt=False) model_checkpoint = ModelCheckpoint( filepath=os.getcwd() + '/weights/ssd7_epoch-{epoch:02d}_loss-{loss:.4f}_val_loss-{val_loss:.4f}.h5', monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1) csv_logger = CSVLogger(filename='ssd7_training_log.csv', separator=',', append=True) early_stopping = EarlyStopping(monitor='val_loss', min_delta=0.0, patience=10, verbose=1) reduce_learning_rate = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=8, verbose=1, epsilon=0.001, cooldown=0, min_lr=0.00001) tf_log = keras.callbacks.TensorBoard(log_dir=TF_LOG_PATH + args.tf_logs, histogram_freq=0, batch_size=batch_size, write_graph=True, write_grads=False, write_images=False) callbacks = [model_checkpoint, csv_logger, reduce_learning_rate, tf_log] # If you're resuming a previous training, set `initial_epoch` and `final_epoch` accordingly. initial_epoch = 0 final_epoch = args.final_epoch steps_per_epoch = 1000 # Train/Fit the model if args.predict_mode == 'train': history = model.fit_generator(generator=train_generator, steps_per_epoch=steps_per_epoch, epochs=final_epoch, callbacks=callbacks, validation_data=val_generator, validation_steps=ceil(val_dataset_size / batch_size), initial_epoch=initial_epoch) # Prediction Output predict_generator = val_dataset.generate( batch_size=1, shuffle=False, transformations=[], label_encoder=ssd_input_encoder, returns={'processed_images', 'processed_labels', 'filenames'}, keep_images_without_gt=False) i = 0 for val in range(val_dataset_size): batch_images, batch_labels, batch_filenames = next(predict_generator) y_pred = model.predict(batch_images) y_pred_decoded = decode_detections( y_pred, confidence_thresh=0.5, iou_threshold=0.5, top_k=200, normalize_coords=Config.normalize_coords, img_height=Config.img_height, img_width=Config.img_width) np.set_printoptions(precision=2, suppress=True, linewidth=90) print("Predicted boxes:\n") print(' class conf xmin ymin xmax ymax') print(y_pred_decoded[i]) plt.figure(figsize=(20, 12)) plt.imshow(batch_images[i]) current_axis = plt.gca() colors = plt.cm.hsv( np.linspace(0, 1, Config.n_classes + 1)).tolist() # Set the colors for the bounding boxes classes = [ 'background', 'polyps' ] # Just so we can print class names onto the image instead of IDs # Draw the ground truth boxes in green (omit the label for more clarity) for box in batch_labels[i]: xmin = box[1] ymin = box[2] xmax = box[3] ymax = box[4] label = '{}'.format(classes[int(box[0])]) current_axis.add_patch( plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, color='green', fill=False, linewidth=2)) current_axis.text(xmin, ymin, label, size='x-large', color='white', bbox={ 'facecolor': 'green', 'alpha': 1.0 }) # Draw the predicted boxes in blue for box in y_pred_decoded[i]: xmin = box[-4] ymin = box[-3] xmax = box[-2] ymax = box[-1] color = colors[int(box[0])] label = '{}: {:.2f}'.format(classes[int(box[0])], box[1]) current_axis.add_patch( plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, color=color, fill=False, linewidth=2)) current_axis.text(xmin, ymin, label, size='x-large', color='white', bbox={ 'facecolor': color, 'alpha': 1.0 }) image = plt.gcf() # plt.show() plt.draw() image.savefig(os.getcwd() + "/val_predictions/val_" + str(val) + ".png", dpi=100)
def predictObject(self): K.clear_session() # Clear previous models from memory. # build object detection model model = build_model(image_size=(self.img_height, self.img_width, self.img_channels), n_classes=self.n_classes, mode='inference', l2_regularization=0.0005, scales=[0.08, 0.16, 0.32, 0.64, 0.96], aspect_ratios_global=[0.5, 1.0, 2.0], aspect_ratios_per_layer=None, two_boxes_for_ar1=True, steps=None, offsets=None, clip_boxes=False, variances=[1.0, 1.0, 1.0, 1.0], normalize_coords=True, subtract_mean=127.5, divide_by_stddev=127.5) # 2: Optional: Load some weights model.load_weights( './trained_weights_ssd7/ssd7_epoch-65_loss-2.1049_val_loss-1.9071.h5', by_name=True) # Two list for storing the original image data and resized image data self.orig_images = [] # Store the original images here. input_images = [] # Store the resized images here. # We'll only load one image in this example. self.orig_images.append(imread(self.image_dir)) img = image.load_img(self.image_dir, target_size=(self.img_height, self.img_width)) img = image.img_to_array(img) input_images.append(img) input_images = np.array(input_images) # predicting image y_pred = model.predict(input_images) # using confidence rate to filter the prediction output y_pred_thresh = [ y_pred[k][y_pred[k, :, 1] > self.confi_threshold] for k in range(y_pred.shape[0]) ] if len(y_pred_thresh) == 1 and len(y_pred_thresh[0]) == 0: self.firstTry = False img_encode = cv2.imencode('.jpg', self.orig_images[0])[1] encodestr = str(base64.b64encode(img_encode), 'utf-8') diction = {'image': encodestr} return self.posturl(self.url_request, diction) # return 600 # code 600 means a new image is needed # raise ValueError("Object detection failed. Please reupload a new image!") max_prob = 0 for box in y_pred_thresh[0]: if box[1] > max_prob: max_prob = box[1] print(max_prob) for box in y_pred_thresh[0]: if box[1] == max_prob: # Transform the predicted bounding boxes for the 300x480 image to the original image dimensions. self.xmin = max( 0, int(box[2] * self.orig_images[0].shape[1] / self.img_width) - 100) self.ymin = max( 0, int(box[3] * self.orig_images[0].shape[0] / self.img_height) - 20) self.xmax = min( int(box[4] * self.orig_images[0].shape[1] / self.img_width) + 90, self.orig_images[0].shape[1]) self.ymax = min( int(box[5] * self.orig_images[0].shape[0] / self.img_height) + 20, self.orig_images[0].shape[0]) print(self.xmin, self.ymin, self.xmax, self.ymax) # The part of image cropped for containerNum. cropImg = self.orig_images[0][self.ymin:self.ymax, self.xmin:self.xmax] if cropImg is not None: print("Object detection successfully!") else: return 600 # code 600 means a new image is needed # raise ValueError("Object detection failed. Please reupload a new image!") # cv2.imwrite("./test_data/检测3117"+".jpg", cropImg) # with open('/Users/jrr/Downloads/object_detection/test_data/target19.jpg', 'rb') as f: # 以二进制读取本地图片 # data = f.read() # encodestr = str(base64.b64encode(data), 'utf-8') # self.diction = {'img': encodestr} img_encode = cv2.imencode('.jpg', cropImg)[1] encodestr = str(base64.b64encode(img_encode), 'utf-8') diction = {'image': encodestr} return self.posturl(self.url_request, diction)
def predictObject(self): K.clear_session() # Clear previous models from memory. # build object detection model model = build_model(image_size=(self.img_height, self.img_width, self.img_channels), n_classes=self.n_classes, mode='inference', l2_regularization=0.0005, scales=[0.08, 0.16, 0.32, 0.64, 0.96], aspect_ratios_global=[0.5, 1.0, 2.0], aspect_ratios_per_layer=None, two_boxes_for_ar1=True, steps=None, offsets=None, clip_boxes=False, variances=[1.0, 1.0, 1.0, 1.0], normalize_coords=True, subtract_mean=127.5, divide_by_stddev=127.5) # 2: Optional: Load some weights model.load_weights('./trained_weights_ssd7/ssd7_300_300_V1_epoch-70_loss-1.3692_val_loss-1.0694.h5', by_name=True) # Two list for storing the original image data and resized image data self.orig_images = [] # Store the original images here. input_images = [] # Store the resized images here. # We'll only load one image in this example. try: resp = urllib.request.urlopen(self.image_dir) except urllib.error.HTTPError as e: print("识别图片URL路径错误!") return [e.code, e.code] img_orig = np.asarray(bytearray(resp.read()), dtype="uint8") img_orig = cv2.imdecode(img_orig, cv2.IMREAD_COLOR) # self.orig_images.append(img_orig) self.orig_images.append(img_orig) # img_new = cv2.resize(img_orig, (self.img_width, self.img_height), interpolation=cv2.INTER_CUBIC) img_new = cv2.resize(img_orig, (self.img_width, self.img_height), interpolation=cv2.INTER_CUBIC) input_images.append(img_new) input_images = np.array(input_images) # predicting image y_pred = model.predict(input_images) # using confidence rate to filter the prediction output y_pred_thresh = [y_pred[k][y_pred[k, :, 1] > self.confi_threshold] for k in range(y_pred.shape[0])] if len(y_pred_thresh) == 1 and len(y_pred_thresh[0]) == 0: self.firstTry = False img_encode = cv2.imencode('.jpg', self.orig_images[0])[1] encodestr = str(base64.b64encode(img_encode), 'utf-8') diction = {'image': encodestr} return [self.posturl(self.url_request, diction), self.posturl(self.url_request, diction)] max_prob = 0 for box in y_pred_thresh[0]: if box[1] > max_prob: max_prob = box[1] for box in y_pred_thresh[0]: if box[1] == max_prob: # Transform the predicted bounding boxes for the 300x480 image to the original image dimensions. self.xmin = max(0, int(box[2] * self.orig_images[0].shape[1] / self.img_width) - 30) self.ymin = max(0, int(box[3] * self.orig_images[0].shape[0] / self.img_height) - 20) self.xmax = min(int(box[4] * self.orig_images[0].shape[1] / self.img_width) + 30, self.orig_images[0].shape[1]) self.ymax = min(int(box[5] * self.orig_images[0].shape[0] / self.img_height) + 20, self.orig_images[0].shape[0]) # print(self.xmin, self.ymin, self.xmax, self.ymax) # The part of image cropped for containerNum. cropImg_conNum = self.orig_images[0][self.ymin:self.ymax, self.xmin:self.xmax] # cropImg_weight = self.orig_images[0][min(int(box[5] * self.orig_images[0].shape[0] / self.img_height) + 80, # self.orig_images[0].shape[0]):self.orig_images[0].shape[0], self.xmin:self.orig_images[0].shape[1]] cropImg_weight = self.orig_images[0][min(self.ymax + 60, self.orig_images[0].shape[0]):self.orig_images[0].shape[0], self.xmin:self.orig_images[0].shape[1]] if cropImg_conNum is not None: print("Object detection successfully!") else: return [600,600] # code 600 means a new image is needed img_encode_conNum = cv2.imencode('.jpg', cropImg_conNum)[1] encodestr_conNum = str(base64.b64encode(img_encode_conNum), 'utf-8') diction_conNum = {'image': encodestr_conNum} img_encode_weight = cv2.imencode('.jpg', cropImg_weight)[1] encodestr_weight = str(base64.b64encode(img_encode_weight), 'utf-8') diction_weight = {'image': encodestr_weight} result_conNum = self.posturl(self.url_request, diction_conNum) result_weight = self.posturl(self.url_request, diction_weight) # return [self.posturl(self.url_request, diction_conNum), self.posturl(self.url_request, diction_weight)] return [result_weight, result_conNum]
def __init__(self, model_name, n_classes=1, mode='inference'): self.model_name = model_name self.n_classes = n_classes self.mode = mode if self.model_name == 'ssd_7': self.image_size = (300, 300, 3) self.intensity_mean = 127.5 self.intensity_range = 127.5 self.scales = [0.08, 0.16, 0.32, 0.64, 0.96] self.aspect_ratios_per_layer = None self.two_boxes_for_ar1 = True self.steps = None self.offsets = None self.clip_boxes = False self.variances = [1.0, 1.0, 1.0, 1.0] self.normalize_coords = True self.model = build_model( image_size=self.image_size, n_classes=self.n_classes, mode=self.mode, l2_regularization=0.0005, scales=self.scales, aspect_ratios_global=[0.5, 1.0, 2.0], aspect_ratios_per_layer=self.aspect_ratios_per_layer, two_boxes_for_ar1=self.two_boxes_for_ar1, steps=self.steps, offsets=self.offsets, clip_boxes=self.clip_boxes, variances=self.variances, normalize_coords=self.normalize_coords, subtract_mean=self.intensity_mean, divide_by_stddev=self.intensity_range) elif self.model_name == 'ssd_300': self.image_size = (300, 300, 3) self.mean_color = [123, 117, 104] self.swap_channels = [2, 1, 0] self.scales = [0.1, 0.2, 0.37, 0.54, 0.71, 0.88, 1.05] self.aspect_ratios_per_layer = [[1.0, 2.0, 0.5], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5], [1.0, 2.0, 0.5]] self.two_boxes_for_ar1 = True self.steps = [8, 16, 32, 64, 100, 300] self.offsets = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5] self.clip_boxes = False self.variances = [0.1, 0.1, 0.2, 0.2] self.normalize_coords = True self.model = ssd_300( image_size=self.image_size, n_classes=self.n_classes, mode=self.mode, l2_regularization=0.0005, scales=self.scales, aspect_ratios_per_layer=self.aspect_ratios_per_layer, two_boxes_for_ar1=self.two_boxes_for_ar1, steps=self.steps, offsets=self.offsets, clip_boxes=self.clip_boxes, variances=self.variances, normalize_coords=self.normalize_coords, subtract_mean=self.mean_color, swap_channels=self.swap_channels) elif self.model_name == 'ssd_512': self.image_size = (512, 512, 3) self.mean_color = [123, 117, 104] self.swap_channels = [2, 1, 0] self.scales = [0.07, 0.15, 0.3, 0.45, 0.6, 0.75, 0.9, 1.05] self.aspect_ratios_per_layer = [[1.0, 2.0, 0.5], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5], [1.0, 2.0, 0.5]] self.two_boxes_for_ar1 = True self.steps = [8, 16, 32, 64, 128, 256, 512] self.offsets = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5] self.clip_boxes = False self.variances = [0.1, 0.1, 0.2, 0.2] self.normalize_coords = True self.model = ssd_512( image_size=self.image_size, n_classes=self.n_classes, mode=self.mode, l2_regularization=0.0005, scales=self.scales, aspect_ratios_per_layer=self.aspect_ratios_per_layer, two_boxes_for_ar1=self.two_boxes_for_ar1, steps=self.steps, offsets=self.offsets, clip_boxes=self.clip_boxes, variances=self.variances, normalize_coords=self.normalize_coords, subtract_mean=self.mean_color, swap_channels=self.swap_channels) else: print('creating ssd_7') self.model_name = 'ssd_7' self.image_size = (300, 300, 3) self.intensity_mean = 127.5 self.intensity_range = 127.5 self.scales = [0.08, 0.16, 0.32, 0.64, 0.96] self.aspect_ratios_per_layer = None self.two_boxes_for_ar1 = True self.steps = None self.offsets = None self.clip_boxes = False self.variances = [1.0, 1.0, 1.0, 1.0] self.normalize_coords = True self.model = build_model( image_size=self.image_size, n_classes=self.n_classes, mode=self.mode, l2_regularization=0.0005, scales=self.scales, aspect_ratios_global=[0.5, 1.0, 2.0], aspect_ratios_per_layer=self.aspect_ratios_per_layer, two_boxes_for_ar1=self.two_boxes_for_ar1, steps=self.steps, offsets=self.offsets, clip_boxes=self.clip_boxes, variances=self.variances, normalize_coords=self.normalize_coords, subtract_mean=self.intensity_mean, divide_by_stddev=self.intensity_range)
limit_boxes = False # Whether or not you want to limit the anchor boxes to lie entirely within the image boundaries variances = [ 1.0, 1.0, 1.0, 1.0 ] # The list of variances by which the encoded target coordinates are scaled coords = 'centroids' # Whether the box coordinates to be used should be in the 'centroids' or 'minmax' format, see documentation normalize_coords = False # Whether or not the model is supposed to use relative coordinates that are within [0,1] model = build_model( image_size=(img_height, img_width, img_channels), n_classes=n_classes, l2_regularization=0.0, scales=scales, aspect_ratios_global=aspect_ratios, aspect_ratios_per_layer=None, two_boxes_for_ar1=two_boxes_for_ar1, steps=steps, offsets=offsets, # limit_boxes=limit_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, subtract_mean=subtract_mean, divide_by_stddev=divide_by_stddev, swap_channels=False) # 2: Optional: Load some weights #model.load_weights('./ssd7_weights.h5') # 3: Instantiate an Adam optimizer and the SSD loss function and compile the model
confidence_threshold = 0.9 target_class = None batch_size = 16 # batch size when testing # 1: Build the Keras model K.clear_session() # Clear previous models from memory. model = build_model(image_size=(img_height, img_width, img_channels), n_classes=n_classes, mode='inference', l2_regularization=0.0005, scales=[0.04, 0.08, 0.12, 0.24], aspect_ratios_global=[0.5, 1.0, 2.0], aspect_ratios_per_layer=None, two_boxes_for_ar1=True, steps=None, offsets=None, clip_boxes=False, variances=[1.0, 1.0, 1.0, 1.0], normalize_coords=True, subtract_mean=127.5, divide_by_stddev=127.5, top_k=200) # 2: Load the trained weights into the model. # TODO: Set the path of the trained weights. weights_path = '/opt/Data/tsl/DeepPCB/ssd-PCB-GPP-MaxPooling/ssd7_epoch-490_loss-0.2872.h5' model.load_weights(weights_path, by_name=True)
def main(): model_mode = 'inference' K.clear_session() # Clear previous models from memory. model = build_model(image_size=(Config.img_height, Config.img_width, Config.img_channels), n_classes=Config.n_classes, mode=model_mode, l2_regularization=Config.l2_regularization, scales=Config.scales, aspect_ratios_per_layer=Config.steps, two_boxes_for_ar1=True, steps=Config.steps, offsets=Config.offsets, clip_boxes=False, variances=Config.variances, normalize_coords=Config.normalize_coords, subtract_mean=Config.intensity_mean, swap_channels=[2, 1, 0], confidence_thresh=0.01, iou_threshold=0.45, top_k=200, nms_max_output_size=400) # 2: Load the trained weights into the model. weights_path = os.getcwd() + '/weights/' + args.model_name + ".h5" model.load_weights(weights_path, by_name=True) adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0) model.compile(optimizer=adam, loss=ssd_loss.compute_loss) test_dataset = DataGenerator(load_images_into_memory=True, hdf5_dataset_path=os.getcwd() + "/data/" + args.dataset + '/polyp_test.h5') test_dataset_size = test_dataset.get_dataset_size() print("Number of images in the test dataset:\t{:>6}".format(test_dataset_size)) classes = ['background', 'polyp'] evaluator = Evaluator(model=model, n_classes=Config.n_classes, data_generator=test_dataset, model_mode=model_mode) results = evaluator(img_height=Config.img_height, img_width=Config.img_width, batch_size=args.batch_size, data_generator_mode='resize', round_confidences=False, matching_iou_threshold=0.5, border_pixels='include', sorting_algorithm='quicksort', average_precision_mode='sample', num_recall_points=11, ignore_neutral_boxes=True, return_precisions=True, return_recalls=True, return_average_precisions=True, verbose=True) mean_average_precision, average_precisions, precisions, recalls = results for i in range(1, len(average_precisions)): print("{:<14}{:<6}{}".format(classes[i], 'AP', round(average_precisions[i], 3))) print("{:<14}{:<6}{}".format('', 'mAP', round(mean_average_precision, 3))) m = max((Config.n_classes + 1) // 2, 2) n = 2 fig, cells = plt.subplots(m, n, figsize=(n * 8, m * 8)) val = 0 for i in range(m): for j in range(n): if n * i + j + 1 > Config.n_classes: break cells[i, j].plot(recalls[n * i + j + 1], precisions[n * i + j + 1], color='blue', linewidth=1.0) cells[i, j].set_xlabel('recall', fontsize=14) cells[i, j].set_ylabel('precision', fontsize=14) cells[i, j].grid(True) cells[i, j].set_xticks(np.linspace(0, 1, 11)) cells[i, j].set_yticks(np.linspace(0, 1, 11)) cells[i, j].set_title("{}, AP: {:.3f}".format(classes[n * i + j + 1], average_precisions[n * i + j + 1]), fontsize=16) image = plt.gcf() # plt.show() plt.draw() image.savefig(os.getcwd() + "/test_out/test_" + str(val) + ".png", dpi=100) val += 1