def __init__(self, class_names, model, input_shape):
        self.class_names = class_names
        self.num_classes = len(class_names)
        self.model = model
        self.input_shape = input_shape
        self.bbox_util = BBoxUtility(self.num_classes)
        self.timer = Timer(1, self.timer_callback)
        self.current_time = 0
        self.current_fps = 0
        self.exec_time = None
        self.prev_extra_time = None
        self.extra_time = None
        self.fps_time_slot = list()
        self.is_finish = False

        # Create unique and somewhat visually distinguishable bright
        # colors for the different classes.
        self.class_colors = []
        for i in range(0, self.num_classes):
            # This can probably be written in a more elegant manner
            hue = 255*i/self.num_classes
            col = np.zeros((1,1,3)).astype("uint8")
            col[0][0][0] = hue
            col[0][0][1] = 128 # Saturation
            col[0][0][2] = 255 # Value
            cvcol = cv2.cvtColor(col, cv2.COLOR_HSV2BGR)
            col = (int(cvcol[0][0][0]), int(cvcol[0][0][1]), int(cvcol[0][0][2]))
            self.class_colors.append(col)
Beispiel #2
0
    def __init__(self):
        NUM_CLASSES = 3 + 1
        input_shape = (300, 300, 3)

        #config_string = rospy.get_param("/traffic_light_config")
        #self.config = yaml.load(config_string)
        #self.stop_line_positions = self.config['stop_line_positions']

        # get path to resources
        #path_to_resources = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', '..', '..', 'tlc')
        # "prior boxes" in the paper
        #priors = pickle.load(open(os.path.join(path_to_resources, 'prior_boxes_ssd300.pkl'), 'rb'))
        priors = pickle.load(open('prior_boxes_ssd300.pkl', 'rb'))
        self.bbox_util = BBoxUtility(NUM_CLASSES, priors)

        # Traffic Light Classifier model and its weights
        self.model = SSD300(input_shape, num_classes=NUM_CLASSES)
        #self.model.load_weights(os.path.join(path_to_resources, self.config['classifier_weights_file']), by_name=True)
        #self.model.load_weights('weights.180314.hdf5', by_name=True)
        self.model.load_weights('checkpoints/weights.07-0.70.hdf5',
                                by_name=True)

        # prevent TensorFlow's ValueError when no raised backend
        dummy = np.zeros((1, 300, 300, 3))
        _ = self.model.predict(dummy, batch_size=1, verbose=0)

        # prevent TensorFlow's ValueError when no raised backend
        dummy = np.zeros((1, 300, 300, 3))
        _ = self.model.predict(dummy, batch_size=1, verbose=0)

        self.is_in_progress = False
        self.last_result = TrafficLight.UNKNOWN
Beispiel #3
0
def predict_img(numpy_array, orig_numpy_array):
    # Save the original image for attachment
    scipy.misc.imsave('temp_cat_motion.jpg', np.uint8(orig_numpy_array))
    
    # Number of voc_classes + 1
    NUM_CLASSES = 3
    input_shape=(300, 300, 3)
    # SSD model
    model = SSD300(input_shape, num_classes=NUM_CLASSES)
    model.load_weights('./model/weights.18-0.09.hdf5', by_name=True)
    bbox_util = BBoxUtility(NUM_CLASSES)
    
    # Inception v3 transfer learning model
    model_cnn = load_model(filepath='./model/model_v2.03-0.40.hdf5')
    ssd_img_size=300
    img_size=299

    inputs = []
    images = []

    images.append(orig_numpy_array)
    
    inputs.append(numpy_array.copy())
    inputs = preprocess_input(np.array(inputs))
    preds = model.predict(inputs, batch_size=1, verbose=0)
    results = bbox_util.detection_out(preds)

    cat_inside_image = False
    # If the SSD model does not find a cat, return False
    for i, img in enumerate(images):
        cat_inside_image = ssd_image(img, results, i)
    
    return cat_inside_image
Beispiel #4
0
  def __init__(self, argv):
 
    app_name  = os.path.basename(argv[0])
    name, _ = app_name.split(".")
    inifile = name + ".ini"
    
    print("inifile {}".format(inifile))
    
    parser = configparser.ConfigParser()
    parser.read(inifile)
    self.weightfile  = parser.get("WEIGHT_FILE",     "filename") 
    self.showimage   = int(parser.get("SHOW_IMAGE",   "show"))
    self.confidence  = float(parser.get("DETECTION", "confidence") )
    
    # For example, this may take a string C:/ssd_keras/weights_SSD300.hdf5

    self.classes = ['Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle',
               'Bus', 'Car', 'Cat', 'Chair', 'Cow', 'Diningtable',
               'Dog', 'Horse','Motorbike', 'Person', 'Pottedplant',
               'Sheep', 'Sofa', 'Train', 'Tvmonitor']
 
    self.n_classes = len(self.classes) + 1

    self.input_shape = (300, 300, 3)
    self.model = SSD300v2(self.input_shape, num_classes=self.n_classes)
        
    self.model.load_weights(self.weightfile, by_name=True)
    
    self.bbox_util = BBoxUtility(self.n_classes)
Beispiel #5
0
 def __init__(self):
     #顔検出モデルと年齢・性別検出モデルを復元
     self.age_detector = load_model("transfer_Xception_29.h5")
     NUM_CLASSES = 2
     input_shape = (300, 300, 3)
     priors = pickle.load(open('prior_boxes_ssd300.pkl', 'rb'))
     self.bbox_util = BBoxUtility(NUM_CLASSES, priors)
     self.face_detector = SSD300(input_shape, num_classes=NUM_CLASSES)
     self.face_detector.load_weights('weights.05-3.15.hdf5', by_name=True)
def feature_flow():
    bbox_util = BBoxUtility(NUM_CLASSES)
    raw_inputs, images = load_inputs(image_files)
    inputs = preprocess_input(np.array(raw_inputs))

    dump_activation_layer = 'conv4_2'
    compare_layer_name = 'conv6_2'
    print('dump_activation_layer', dump_activation_layer)
    print('target_layer_name', compare_layer_name)

    # normal SSD network
    model1 = SSD300v2(input_shape, num_classes=NUM_CLASSES)
    model1.load_weights('weights_SSD300.hdf5', by_name=True)
    predictions = run_network(model1, inputs)
    results = bbox_util.detection_out(predictions)
    plot_detections(images, results)

    # get dump layer's output (as input for flow network)
    input_img2 = inputs[1:2, :, :, :]
    layer_dump = get_layer_output(model=model1,
                                  inputs=input_img2,
                                  output_layer_name=dump_activation_layer)
    print('layer_dump.shape = ', layer_dump.shape)

    # flow (raw rgb)
    flow_rgb = compute_flow(image_files[1], image_files[0])

    print('flow.shape', flow_rgb.shape)
    imshow_fig(cv2.cvtColor(draw_hsv(flow_rgb), cv2.COLOR_BGR2RGB),
               title='flow_rgb')

    # flow (re-sized for feature map)
    flow_feature = get_flow_for_filter(flow_rgb)
    # imshow_fig(flow_feature[:, :, 0], title='flow_feature_y', cmap='gray')
    # imshow_fig(flow_feature[:, :, 1], title='flow_feature_x', cmap='gray')

    # warp image by flow_rgb
    iimg1 = cv2.imread(image_files[0])
    img_warp = warp_flow(iimg1, flow_rgb)
    imshow_fig(cv2.cvtColor(img_warp, cv2.COLOR_BGR2RGB), title='frame_2_warp')

    # shift feature
    shifted_feature = shift_filter(layer_dump, flow_feature)

    # flow net
    model2 = SSD300_conv4_3((128, 128, 512), num_classes=NUM_CLASSES)
    model2.load_weights('weights_SSD300.hdf5', by_name=True)
    predictions = run_network(model2, shifted_feature)
    results = bbox_util.detection_out(predictions)
    plot_detections(images[1:2], results)

    # get specific layer's output and compare them (for debugging)
    compare_model_layer(model1, input_img2, compare_layer_name, model2,
                        shifted_feature, compare_layer_name, True)

    sess.close()
    plt.show()
    def __init__(self):
        self.node_name = "ssd_keras"
        rospy.init_node(self.node_name)
        self.class_names = [
            "background", "aeroplane", "bicycle", "bird", "boat", "bottle",
            "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse",
            "motorbike", "person", "pottedplant", "sheep", "sofa", "train",
            "tvmonitor"
        ]
        self.num_classes = len(self.class_names)
        self.input_shape = (300, 300, 3)
        self.model = SSD(self.input_shape, num_classes=self.num_classes)
        self.model.load_weights(pkg_path +
                                '/resources/ssd_keras/weights_SSD300.hdf5')

        self.bbox_util = BBoxUtility(self.num_classes)
        self.conf_thresh = 0.25

        self.model._make_predict_function()
        self.graph = tf.get_default_graph()

        self.detection_index = DL_msgs_boxes()

        # Create unique and somewhat visually distinguishable bright
        # colors for the different classes.
        self.class_colors = []
        for i in range(0, self.num_classes):
            # This can probably be written in a more elegant manner
            hue = 255 * i / self.num_classes
            col = np.zeros((1, 1, 3)).astype("uint8")
            col[0][0][0] = hue
            col[0][0][1] = 128  # Saturation
            col[0][0][2] = 255  # Value
            cvcol = cv2.cvtColor(col, cv2.COLOR_HSV2BGR)
            col = (int(cvcol[0][0][0]), int(cvcol[0][0][1]),
                   int(cvcol[0][0][2]))
            self.class_colors.append(col)

        self.bridge = CvBridge()  # Create the cv_bridge object

        self.Image_Status = "Not_Ready"
        self.StartImage = cv2.imread(pkg_path + '/resources/start.jpg')
        self.to_draw = cv2.resize(self.StartImage, (640, 480))

        self.image_sub = rospy.Subscriber(
            "/floating_sensor/camera/rgb/image_raw",
            Image,
            self.detect_image,
            queue_size=1)  # the appropriate callbacks

        self.box_coordinate_pub = rospy.Publisher(
            "/ssd_detction/box", DL_msgs_boxes,
            queue_size=5)  # the appropriate callbacks
        self.SSD_Serv = rospy.Service('SSD_Detection', DL_box,
                                      self.SSD_Detection_Server)
Beispiel #8
0
 def __init__(
         self,
         class_number=21,
         input_shape=(300, 300, 3),
         priors_file='prior_boxes_ssd300.pkl',
         train_file='VOC2007.pkl',
         path_prefix='./VOCdevkit/VOC2007/JPEGImages/',
         model=None,
         weight_file='weights_SSD300.hdf5',
         freeze=('input_1', 'conv1_1', 'conv1_2', 'pool1', 'conv2_1',
                 'conv2_2', 'pool2', 'conv3_1', 'conv3_2', 'conv3_3',
                 'pool3'),
         save_weight_file='/src/resource/checkpoints/weights.{epoch:02d}-{val_loss:.2f}.hdf5',  # noqa
         optim=None,
         batch_size=20,
         nb_worker=1):
     """
     Setting below parameter 
     :param class_number(int): class number
     :param input_shape(set): set input shape  
     :param priors_file(str): set prior file name 
     :param train_file(str): train file name  
     :param path_prefix(str): path prefix 
     :param model(keras model): set the keras model such as the ssd 
     :param weight_file(str): weight file name 
     :param freeze(set): set untraining layer 
     """
     self.input_shape = input_shape
     priors = pickle.load(open(priors_file, 'rb'))
     self.bbox_utils = BBoxUtility(class_number, priors)
     self.train_data = pickle.load(open(train_file, 'rb'))
     keys = sorted(self.train_data.keys())
     num_train = int(round(0.8 * len(keys)))
     self.train_keys = keys[:num_train]
     self.val_keys = keys[num_train:]
     self.num_val = len(self.val_keys)
     self.batch_size = batch_size
     self.gen = Generator(self.train_data,
                          self.bbox_utils,
                          batch_size,
                          path_prefix,
                          self.train_keys,
                          self.val_keys,
                          (self.input_shape[0], self.input_shape[1]),
                          do_crop=True)
     self.model = model
     model.load_weights(weight_file, by_name=True)
     self.freeze = list(freeze)
     self.save_weight_file = save_weight_file
     self.optim = optim
     self.nb_worker = nb_worker
     self.model.compile(optimizer=optim,
                        metrics=['accuracy'],
                        loss=MultiboxLoss(class_number,
                                          neg_pos_ratio=2.0).compute_loss)
Beispiel #9
0
    def __init__(self):
        #TODO load classifier
        NUM_CLASSES = 3 + 1
        input_shape = (300, 300, 3)

        # "prior boxes" in the paper
        priors = pickle.load(open('prior_boxes_ssd300.pkl', 'rb'))
        self.bbox_util = BBoxUtility(NUM_CLASSES, priors)

        self.model = SSD300(input_shape, num_classes=NUM_CLASSES)
        self.model.load_weights('weights.180314.hdf5', by_name=True)
Beispiel #10
0
	def __init__(self, input_shape = (300, 300, 3)):
		self.num_class = config.NUM_CLASSES
		self.input_tensor = tf.placeholder(tf.float32, [None, input_shape[0], input_shape[1], input_shape[2]])
		self.label_tensor = tf.placeholder(tf.float32, [None, 7308, 4 + config.NUM_CLASSES + 8])
		self.predicts = self.build(input_shape, config.NUM_CLASSES)
		self.input_shape = input_shape
		self.global_step = tf.train.create_global_step()
		var_list = tf.global_variables()
		var_list = [var for var in var_list if "Adam" not in var.name]
		self.saver = tf.train.Saver(var_list, max_to_keep=1)
		self.bbox_util = BBoxUtility(self.num_class)
Beispiel #11
0
    def __init__(self):

        voc_classes = ['Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle',
               'Bus', 'Car', 'Cat', 'Chair', 'Cow', 'Diningtable',
               'Dog', 'Horse','Motorbike', 'Person', 'Pottedplant',
               'Sheep', 'Sofa', 'Train', 'Tvmonitor']
        NUM_CLASSES = len(voc_classes) + 1

        input_shape=(300, 300, 3)
        self.model = SSD300(input_shape, num_classes=NUM_CLASSES)
        weights_file = "./checkpoints/weights.10-2.85.hdf5"        
        #weights_file = "./checkpoints/weights.39-1.61_ubuntu.hdf5"

        self.model.load_weights(weights_file, by_name=True)
        self.bbox_util = BBoxUtility(NUM_CLASSES)
Beispiel #12
0
    def __init__(self):
        self.image_width = 300
        self.image_height = 300

        self.voc_classes = [
            'Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle', 'Bus', 'Car',
            'Cat', 'Chair', 'Cow', 'Diningtable', 'Dog', 'Horse', 'Motorbike',
            'Person', 'Pottedplant', 'Sheep', 'Sofa', 'Train', 'Tvmonitor'
        ]
        self.NUM_CLASSES = len(self.voc_classes) + 1

        self.model = SSD300((self.image_height, self.image_width, 3),
                            num_classes=self.NUM_CLASSES)
        self.model.load_weights('weights_SSD300.hdf5', by_name=True)
        self.bbox_util = BBoxUtility(self.NUM_CLASSES)
def init_model(weight_file='ssd_keras/SSD/weights_SSD300.hdf5'):

    np.set_printoptions(suppress=True)

    config = tf.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.45
    set_session(tf.Session(config=config))

    NUM_CLASSES = len(voc_classes) + 1

    input_shape = (300, 300, 3)
    model = SSD300(input_shape, num_classes=NUM_CLASSES)
    model.load_weights(weight_file, by_name=True)
    bbox_util = BBoxUtility(NUM_CLASSES)
    return model, bbox_util
Beispiel #14
0
    def __init__(self,
                 modelfile,
                 shape=(300, 300, 3),
                 num_classes=21,
                 conf_thresh=0.6):

        self.input_shape = shape
        self.num_classes = num_classes
        self.conf_thresh = conf_thresh

        # モデル作成
        model = SSD(shape, num_classes=num_classes)
        model.load_weights(modelfile)
        self.model = model

        # バウンディングボックス作成ユーティリティ
        self.bbox_util = BBoxUtility(self.num_classes)
Beispiel #15
0
	def train(self):
		self.loss = MultiboxLoss(self.num_class, neg_pos_ratio=2.0).compute_loss(self.label_tensor, self.predicts)
		self.loss_avg = tf.reduce_mean(self.loss)
		
		learning_rate = tf.train.exponential_decay(config.lr, self.global_step, 10000 ,0.9, True, name='learning_rate')
		self.train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(self.loss, global_step = self.global_step)
		self.train_loss_summary = tf.summary.scalar("loss_train", self.loss_avg)
		self.val_loss_summary = tf.summary.scalar("loss_val", self.loss_avg)
		self.writer = tf.summary.FileWriter(FLAGS.checkpoint)

		priors = pickle.load(open('prior_boxes_ssd300.pkl', 'rb'))
		self.bbox_util = BBoxUtility(self.num_class, priors)

		gt = pickle.load(open(FLAGS.label_file, 'rb'))
		keys = sorted(gt.keys())
		num_train = int(round(0.8 * len(keys)))
		train_keys = keys[:num_train]
		val_keys = keys[num_train:]

		gen = Generator(gt, self.bbox_util, config.BATCH_SIZE, FLAGS.images_dir,
		                train_keys, val_keys,
		                (self.input_shape[0], self.input_shape[1]))#, do_crop=False, saturation_var = 0, brightness_var = 0, contrast_var = 0, lighting_std = 0, hflip_prob = 0, vflip_prob = 0)
		c = tf.ConfigProto()
		c.gpu_options.allow_growth = True
		with tf.Session(config=c) as sess:
			sess.run(tf.global_variables_initializer())
			self.writer.add_graph(sess.graph)
			self.restore(sess)
			for inputs, labels in gen.generate(True):
				_, lo, step, summary = sess.run([self.train_op, self.loss_avg, self.global_step, self.train_loss_summary], feed_dict = {self.input_tensor: inputs, self.label_tensor: labels})
				sys.stdout.write("train loss: %d %.3f \r"%(step, lo))
				sys.stdout.flush()
				self.writer.add_summary(summary, step)
				if step % config.save_step == config.save_step - 1:
					self.saver.save(sess, os.path.join(FLAGS.checkpoint, "ckpt"), global_step=self.global_step)
					print("saved")
				if step % config.snapshot_step == 0:
					val_in, val_la = next(gen.generate(False))
					lo, s, preds = sess.run([self.loss_avg, self.train_loss_summary, self.predicts], feed_dict = {self.input_tensor: val_in, self.label_tensor: val_la})
					self.writer.add_summary(s, step)
					print("val loss:", step, lo)
					images = [np.array(val_in[v]) for v in range(val_in.shape[0])]
					self.paint_imgs(preds, images)

		print("Train finished. Checkpoint saved in", FLAGS.checkpoint)
Beispiel #16
0
    def __init__(self, conf_limit=0.6):
        self.conf_limit = conf_limit
        np.set_printoptions(suppress=True)
        config = tf.ConfigProto()
        #config.gpu_options.per_process_gpu_memory_fraction = 0.45
        set_session(tf.Session(config=config))

        self.voc_classes = [
            'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car',
            'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike',
            'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'
        ]
        NUM_CLASSES = len(self.voc_classes) + 1
        self.bbox_util = BBoxUtility(NUM_CLASSES)

        input_shape = (300, 300, 3)
        self.model = SSD300(input_shape, num_classes=NUM_CLASSES)
        self.model.load_weights('weights_SSD300.hdf5', by_name=True)
Beispiel #17
0
    def __init__(
            self,
            path_weights="/home/francisco/git/ssd_keras/weights_SSD300.hdf5"):
        config = tf.ConfigProto()
        config.gpu_options.per_process_gpu_memory_fraction = 0.45
        set_session(tf.Session(config=config))

        self.labels = [
            'Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle', 'Bus', 'Car',
            'Cat', 'Chair', 'Cow', 'Diningtable', 'Dog', 'Horse', 'Motorbike',
            'Person', 'Pottedplant', 'Sheep', 'Sofa', 'Train', 'Tvmonitor'
        ]
        NUM_CLASSES = len(self.labels) + 1

        input_shape = (300, 300, 3)
        # Get detections with confidence higher than 0.6.
        self.detection_confidence = 0.6
        self.model = SSD300(input_shape, num_classes=NUM_CLASSES)
        self.model.load_weights(path_weights, by_name=True)
        self.bbox_util = BBoxUtility(NUM_CLASSES)
        self.detections = []
Beispiel #18
0
def predict_img(numpy_array, orig_numpy_array):
    # Save the original image for attachment
    scipy.misc.imsave('temp_cat_water.jpg', np.uint8(orig_numpy_array))

    # Number of voc_classes + 1
    NUM_CLASSES = 3
    input_shape = (300, 300, 3)
    # SSD model
    model = SSD300(input_shape, num_classes=NUM_CLASSES)
    model.load_weights('./model/weights.18-0.09.hdf5', by_name=True)
    bbox_util = BBoxUtility(NUM_CLASSES)

    # Inception v3 transfer learning model
    model_cnn = load_model(filepath='./model/model_v2.03-0.40.hdf5')
    ssd_img_size = 300
    img_size = 299

    inputs = []
    images = []

    images.append(orig_numpy_array)

    inputs.append(numpy_array.copy())
    inputs = preprocess_input(np.array(inputs))
    preds = model.predict(inputs, batch_size=1, verbose=0)
    results = bbox_util.detection_out(preds)

    # If the SSD model does not find an appropriate object, automatically return 0.00
    for i, img in enumerate(images):
        if type(results[i]) is not list:
            ssd_img = ssd_image(img, results, i)
            resize_img = imresize(ssd_img, (img_size, img_size))

            x = np.expand_dims(resize_img, axis=0)
            y_pred = model_cnn.predict(x)
            prediction = round(y_pred[0][0], 3)
        else:
            prediction = 0.00

    return prediction
Beispiel #19
0
 def __init__(self, class_names, model, input_shape, confidence):  # {{{
     self.class_names = class_names
     self.num_classes = len(class_names)
     self.model = model
     self.input_shape = input_shape
     self.confidence = confidence
     self.bbox_util = BBoxUtility(self.num_classes)
     self.next_ID = 0
     # Create unique and somewhat visually distinguishable bright
     # colors for the different classes.
     self.class_colors = []
     for i in range(0, self.num_classes):
         # This can probably be written in a more elegant manner
         hue = 255 * i / self.num_classes
         col = np.zeros((1, 1, 3)).astype("uint8")
         col[0][0][0] = hue
         col[0][0][1] = 128  # Saturation
         col[0][0][2] = 255  # Value
         cvcol = cv2.cvtColor(col, cv2.COLOR_HSV2BGR)
         col = (int(cvcol[0][0][0]), int(cvcol[0][0][1]),
                int(cvcol[0][0][2]))
         self.class_colors.append(col)  # }}}
Beispiel #20
0
def main(img_paths):
    """
    Detect objects in images.

    Parameters
    ----------
    img_paths : list of strings
    """
    # Load the model
    voc_classes = [
        'Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle', 'Bus', 'Car', 'Cat',
        'Chair', 'Cow', 'Diningtable', 'Dog', 'Horse', 'Motorbike', 'Person',
        'Pottedplant', 'Sheep', 'Sofa', 'Train', 'Tvmonitor'
    ]
    NUM_CLASSES = len(voc_classes) + 1
    input_shape = (300, 300, 3)
    model = SSD300(input_shape, num_classes=NUM_CLASSES)
    model.load_weights('weights_SSD300.hdf5', by_name=True)
    bbox_util = BBoxUtility(NUM_CLASSES)

    # Load the inputs
    inputs = []
    images = []
    for img_path in img_paths:
        img = image.load_img(img_path, target_size=(300, 300))
        img = image.img_to_array(img)
        images.append(imread(img_path))
        inputs.append(img.copy())
    inputs = preprocess_input(np.array(inputs))

    # Predict
    preds = model.predict(inputs, batch_size=1, verbose=1)
    results = bbox_util.detection_out(preds)

    # Visualize
    for i, img in enumerate(images):
        create_overlay(img, results[i], voc_classes,
                       "{}-det.png".format(img_paths[i]))
Beispiel #21
0
    def __init__(self):
        NUM_CLASSES = 3 + 1
        input_shape = (300, 300, 3)

        config_string = rospy.get_param("/traffic_light_config")
        self.config = yaml.load(config_string)
        self.stop_line_positions = self.config['stop_line_positions']

        # get path to resources
        path_to_resources = os.path.join(
            os.path.dirname(os.path.abspath(__file__)), '..', '..', '..', '..',
            'tlc')
        # "prior boxes" in the paper
        priors = pickle.load(
            open(os.path.join(path_to_resources, 'prior_boxes_ssd300.pkl'),
                 'rb'))
        self.bbox_util = BBoxUtility(NUM_CLASSES, priors)

        # Traffic Light Classifier model and its weights
        self.model = SSD300(input_shape, num_classes=NUM_CLASSES)
        print(self.model.summary())
        self.model.load_weights(os.path.join(
            path_to_resources, self.config['classifier_weights_file']),
                                by_name=True)

        # prevent TensorFlow's ValueError when no raised backend
        dummy = np.zeros((1, 300, 300, 3))
        _ = self.model.predict(dummy, batch_size=1, verbose=0)

        # prevent TensorFlow's ValueError when no raised backend
        dummy = np.zeros((1, 300, 300, 3))
        _ = self.model.predict(dummy, batch_size=1, verbose=0)

        self.capture_images = False
        self.image_counts = {0: 0, 1: 0, 2: 0, 4: 0}

        self.last_classification = None
Beispiel #22
0
            for i in range(nb_epoch):
                fp.write("%d\t%f\t%f\t%f\t%f\n" %
                         (epochs, loss[i], acc[i], val_loss[i], val_acc[i]))


plt.rcParams['figure.figsize'] = (8, 8)
plt.rcParams['image.interpolation'] = 'nearest'

np.set_printoptions(suppress=True)

# 21
NUM_CLASSES = 21  #4
input_shape = (300, 300, 3)

priors = pickle.load(open('prior_boxes_ssd300.pkl', 'rb'))
bbox_util = BBoxUtility(NUM_CLASSES, priors)

# gt = pickle.load(open('gt_pascal.pkl', 'rb'))
gt = pickle.load(open('VOC2007.pkl', 'rb'))
keys = sorted(gt.keys())
num_train = int(round(0.8 * len(keys)))
train_keys = keys[:num_train]
val_keys = keys[num_train:]
num_val = len(val_keys)


class Generator(object):
    def __init__(self,
                 gt,
                 bbox_util,
                 batch_size,
Beispiel #23
0
if __name__ == "__main__":
    import glob
    imagesList = glob.glob("images/*.jpg")

    # Load the model
    voc_classes = [
        'Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle', 'Bus', 'Car', 'Cat',
        'Chair', 'Cow', 'Diningtable', 'Dog', 'Horse', 'Motorbike', 'Person',
        'Pottedplant', 'Sheep', 'Sofa', 'Train', 'Tvmonitor'
    ]
    NUM_CLASSES = len(voc_classes) + 1
    input_shape = (300, 300, 3)
    model = SSD300(input_shape, num_classes=NUM_CLASSES)
    model.load_weights('weights_SSD300.hdf5', by_name=True)
    bbox_util = BBoxUtility(NUM_CLASSES)

    # Load the inputs
    inputs = []
    images = []
    for img_path in imagesList:
        print("process " + img_path)
        img = image.load_img(img_path, target_size=(300, 300))
        img = image.img_to_array(img)
        images.append(imread(img_path))
        inputs.append(img.copy())
    # 前置處理
    print("前置處理...")
    inputs = preprocess_input(np.array(inputs))

    # 預測
Beispiel #24
0
def main(dataset, run, input_shape, seq_start, seq_stop, videopath, conf_thresh, i_seq, outname, batch_size):
    
    print_flush("> Predicting...")
    classes = get_classnames(dataset)
    masker = Masker(dataset)
    
    input_shape = parse_resolution(input_shape)
    
    num_classes = len(classes)+1
    model = get_model(dataset, run, input_shape, num_classes, verbose=False)
    priors = get_priors(model, input_shape)
    bbox_util = BBoxUtility(num_classes, priors)
    
    
    width = input_shape[0]
    height = input_shape[1]
    
    inputs = []
    outputs = []
    old_frame = None
    
    with io.get_reader(videopath) as vid: 
        vlen = len(vid)
        for i_in_seq in range(seq_start, seq_stop):
            if i_in_seq < vlen:
                frame = vid.get_data(i_in_seq)
                frame = masker.mask(frame)
                old_frame = frame
            else:
                frame = old_frame
                
            resized = cv2.resize(frame, (width, height))
            inputs.append(resized)
            
            if len(inputs) == batch_size:
                inputs2 = np.array(inputs)
                inputs2 = inputs2.astype(np.float32)
                inputs2 = preprocess_input(inputs2)
                
                y = model.predict_on_batch(inputs2)
                outputs.append(y)
                
                inputs = []     
        
    preds = np.vstack(outputs)
    
    print_flush("> Processing...")
    all_detections = []   
    seq_len = seq_stop - seq_start
         
    for i in range(seq_len):
        frame_num = i + seq_start
        
        if frame_num < vlen:           
            pred = preds[i, :]
            pred = pred.reshape(1, pred.shape[0], pred.shape[1])
            results = bbox_util.detection_out(pred, soft=False)

            detections = process_results(results, width, height, classes, conf_thresh, frame_num)
            all_detections.append(detections)
    
    dets = pd.concat(all_detections)
    
    # For the first line, we should open in write mode, and then in append mode
    # This way, we still overwrite the files if this script is run multiple times
    open_mode = 'a'
    include_header = False
    if i_seq == 0:
        open_mode = 'w'
        include_header = True

    print_flush("> Writing to {} ...".format(outname))    
    with open(outname, open_mode) as f:
        dets.to_csv(f, header=include_header) 
def train_SSD300_NAG(
        master_file,
        train_dir,
        test_dir,
        model_path,
        load_weights_path=r'C:\Users\shingo\jupyter_notebook\tfgpu_py36_work\AI_Edge_Contest\object_detection\SSD_classes_py\all_SSD_module\SSD\weights_SSD300.hdf5',
        epochs=20,
        batch_size=32,
        base_lr=1e-3,
        num_classes=6 + 1,
        callback=[]):
    """
    dtc_train.py のパラメータなどを引数にした関数
    ラベル情報のcsvファイルから訓練画像の領域情報ロードし、SSDのモデル作成する
    ※csvファイルからラベル情報読めるのが良いところ(一般的な物体検出モデルのラベル情報は1画像1xmlファイル)
    画像のサイズは300x300に変換される(ssd_vgg.pyより)
    分類器はVGG16のfine-tuning
    オプティマイザは ネステロフ+モメンタム+SGD(decayあり). 学習率はLearningRateScheduler でも下げる
    Args:
        master_file : 正解の座標(ファイル名, x, y, width, height, ラベルid)一覧のcsvファイルパス.
                      SSDの「背景」ラベルとして使われるため、ラベルidは0を使わないこと!!!
        train_dir : 訓練用画像が入っているフォルダパス
        test_dir : 評価用画像が入っているフォルダパス
        model_path : モデルファイルの保存先パス
        load_weights_path : 重みファイルのパス
        epochs : エポック数
        batch_size : バッチサイズ
        base_lr : 学習率初期値
        num_classes : クラス数。クラス数は「背景(class_id=0固定)」と「分類したいクラス」の数(要するにクラス数+1)にしないと正しくできない!!!!
        callback: 追加するcallbackのリスト。空なら ModelCheckpoint と LearningRateScheduler だけの callback にになる
    Return:
        なし(モデルファイルweight_ssd_best.hdf5 出力)
    """

    #epochs = 20        # エポック数
    #batch_size = 32     # バッチサイズ
    #base_lr =  1e-3     # 学習率初期値
    #num_classes = 11

    # 最適化関数
    # optimizer = keras.optimizers.Adam(lr=base_lr)
    # optimizer = keras.optimizers.RMSprop(lr=base_lr)
    optimizer = keras.optimizers.SGD(lr=base_lr,
                                     momentum=0.9,
                                     decay=1e-6,
                                     nesterov=True)

    # 学習率のスケジュール関数
    def schedule(epoch, decay=0.90):
        return base_lr * decay**(epoch)

    # 正解の座標(ファイル名, x, y, width, height)一覧のcsvファイル
    #master_file = "xywh_train.csv"
    # 訓練用画像が入っているフォルダ
    #train_dir = "ssd_train"
    # 評価用画像が入っているフォルダ
    #test_dir = "ssd_test"

    # 画像ファイル名を指定すると正解座標が返ってくる辞書を作成
    correct_boxes = get_correct_boxes(master_file,
                                      train_dir,
                                      test_dir,
                                      num_classes=num_classes)

    # 画像ファイルパス一覧取得
    train_path_list = glob.glob(os.path.join(train_dir, "*.*"))
    test_path_list = glob.glob(os.path.join(test_dir, "*.*"))
    ## 画像ファイルパス一覧取得
    #train_path_list = []
    #test_path_list = []
    #for folder in glob.glob(os.path.join(train_dir, "*")):
    #    for file in glob.glob(os.path.join(folder, "*.jpg")):
    #        train_path_list.append(file)
    #for folder in glob.glob(os.path.join(test_dir, "*")):
    #    for file in glob.glob(os.path.join(folder, "*.jpg")):
    #        test_path_list.append(file)

    # モデル作成
    model = create_model(num_classes=num_classes)
    print('create_model ok')
    model.load_weights(load_weights_path, by_name=True)
    print('load_weights ok')

    # 入力付近の層をフリーズ
    freeze_layers(model, depth_level=1)
    print('freeze_layers ok')

    model.compile(optimizer=optimizer,
                  loss=MultiboxLoss(num_classes).compute_loss)
    #model.summary()
    plot_model(model, os.path.join(os.path.dirname(model_path),
                                   "model_ssd.png"))

    # デフォルトボックス作成
    priors = create_prior_box()

    # 画像データのジェネレータ作成
    bbox_util = BBoxUtility(num_classes, priors)
    gen = Generator(correct_boxes, bbox_util, train_path_list, test_path_list,
                    (input_shape[0], input_shape[1]), batch_size)

    print("Train Items : {}".format(gen.train_batches))
    print("Test  Items : {}".format(gen.val_batches))

    # コールバック設定
    callbacks = [
        ModelCheckpoint(model_path,
                        verbose=1,
                        save_weights_only=True,
                        save_best_only=True)
    ]  #, LearningRateScheduler(schedule)]
    if len(callback) != 0:
        callbacks.extend(callback)

    #print(model.summary())

    # 学習開始
    start_time = time.time()
    history = model.fit_generator(gen.generate(True),
                                  gen.train_batches // batch_size,
                                  epochs=epochs,
                                  verbose=2,
                                  callbacks=callbacks,
                                  validation_data=gen.generate(False),
                                  validation_steps=gen.val_batches //
                                  batch_size)
    end_time = time.time()

    # 経過時間表示
    elapsed_time = end_time - start_time
    print("Elapsed Time : {0:d} hr {1:d} min {2:d} sec".format(
        int(elapsed_time // 3600), int((elapsed_time % 3600) // 60),
        int(elapsed_time % 60)))
    return history
Beispiel #26
0
        img = image.img_to_array(img)

        images.append(img)
        inputs.append(img.copy())

        inputs = preprocess_input(np.array(inputs))
        dir_path = os.path.dirname(os.path.realpath(__file__))

        priors = pickle.load(
            open(
                os.path.join(
                    dir_path,
                    'priorFiles/prior_boxes_ssd300MobileNetV2_224_224.pkl'),
                'rb'))

        bbox_util = BBoxUtility(21, priors)

        preds = tf_inference.predict(inputs)
        results = bbox_util.detection_out(preds)

        for i, img in enumerate(images):
            # Parse the outputs.
            det_label = results[i][:, 0]
            det_conf = results[i][:, 1]
            det_xmin = results[i][:, 2]
            det_ymin = results[i][:, 3]
            det_xmax = results[i][:, 4]
            det_ymax = results[i][:, 5]

            # Get detections with confidence higher than 0.6.
            top_indices = [i for i, conf in enumerate(det_conf) if conf >= 0.6]
Beispiel #27
0
def test_on_video(model,
                  name,
                  experiment,
                  videopath,
                  outvideopath,
                  classnames,
                  batch_size=32,
                  input_shape=(480, 640, 3),
                  soft=False,
                  width=480,
                  height=640,
                  conf_thresh=0.75,
                  csv_conf_thresh=0.75):
    """ Applies a trained SSD model to a video
    
    Arguments:
    model           -- the SSD model, e.g. from get_model
    name            -- name of dataset
    experiment      -- name of training run
    videopath       -- path to input video
    outvideopath    -- path to output video showing the detections
    classnames      -- list of all the classes
    batch_size      -- number of images processed in parallell, lower this if you get out-of-memory errors
    input_shape     -- size of images fed to SSD
    soft            -- Whether to do soft NMS or normal NMS
    width           -- Width to scale detections with (can be set to 1 if detections are already on right scale)
    height          -- Height to scale detections with (can be set to 1 if detections are already on right scale)
    conf_thresh     -- Detections with confidences below this are not shown in output video. Set to negative to not visualize confidences.
    csv_conf_thresh -- Detections with confidences below this are ignored. This should be same as conf_thresh unless conf_thresh is negative.
    
    """
    masker = Masker(name)

    num_classes = len(classnames) + 1
    colors = class_colors(num_classes)

    make_vid = True
    suffix = outvideopath.split('.')[-1]
    if suffix == 'csv':
        make_vid = False
        csvpath = outvideopath
    else:
        csvpath = outvideopath.replace('.{}'.format(suffix), '.csv')

    print_flush('Generating priors')
    im_in = np.random.random(
        (1, input_shape[1], input_shape[0], input_shape[2]))
    priors = model.predict(im_in, batch_size=1)[0, :, -8:]
    bbox_util = BBoxUtility(num_classes, priors)

    vid = io.get_reader(videopath)
    if make_vid:
        outvid = io.get_writer(outvideopath, fps=30)

    inputs = []
    frames = []

    all_detections = []
    for i, frame in enumerate(vid):
        frame = masker.mask(frame)
        resized = cv2.resize(frame, (input_shape[0], input_shape[1]))

        frames.append(frame.copy())
        inputs.append(resized)

        if len(inputs) == batch_size:
            inputs = np.array(inputs).astype(np.float64)
            inputs = preprocess_input(inputs)

            preds = model.predict(inputs, batch_size=batch_size, verbose=0)
            results = bbox_util.detection_out(preds, soft=soft)

            for result, frame, frame_number in zip(results, frames,
                                                   range(i - batch_size, i)):
                result = [
                    r if len(r) > 0 else np.zeros((1, 6)) for r in result
                ]
                raw_detections = pd.DataFrame(np.vstack(result),
                                              columns=[
                                                  'class_index', 'confidence',
                                                  'xmin', 'ymin', 'xmax',
                                                  'ymax'
                                              ])

                rescale(raw_detections, 'xmin', width)
                rescale(raw_detections, 'xmax', width)
                rescale(raw_detections, 'ymin', height)
                rescale(raw_detections, 'ymax', height)
                rescale(raw_detections, 'class_index', 1)

                ci = raw_detections['class_index']
                cn = [classnames[int(x) - 1] for x in ci]
                raw_detections['class_name'] = cn

                raw_detections['frame_number'] = (frame_number + 2)
                all_detections.append(raw_detections[
                    raw_detections.confidence > csv_conf_thresh])

                if make_vid:
                    frame = draw(frame,
                                 raw_detections,
                                 colors,
                                 conf_thresh=conf_thresh)
                    outvid.append_data(frame)

            frames = []
            inputs = []

        if i % (10 * batch_size) == 0:
            print_flush(i)

    detections = pd.concat(all_detections)

    detections.to_csv(csvpath)
Beispiel #28
0
from ssd import SSD300
from ssd_utils import BBoxUtility
from ssd_training import MultiboxLoss
from pycocotools.coco import COCO
import numpy as np
from scipy.misc import imread
from scipy.misc import imresize
import pickle

model = SSD300(num_classes=81)

mbLoss = MultiboxLoss(num_classes=81)
model.compile(loss=mbLoss.compute_loss, optimizer='adam')

ssd300_priors = pickle.load(open('prior_boxes_ssd300.pkl'))
bboxer = BBoxUtility(num_classes=81, priors=ssd300_priors)

cocodata = COCO('/DATA/COCO/annotations/instances_train2017.json')
cocoDir = '/DATA/COCO/train2017/'
catsToIds = {}

for i, catid in enumerate(cocodata.getCatIds()):
    catsToIds[catid] = i


def generator(batch_size=4):
    imgList = cocodata.imgs.keys()
    imgcount = len(imgList)
    n = 0
    while True:
        X = np.zeros((batch_size, 300, 300, 3), dtype=np.float)
def run_camera(input_shape, model, save_path, frame_number):
    num_classes = 21
    conf_thresh = 0.4
    bbox_util = BBoxUtility(num_classes)

    class_colors = []
    for i in range(0, num_classes):
        hue = 255 * i / num_classes
        col = np.zeros((1, 1, 3)).astype("uint8")
        col[0][0][0] = hue
        col[0][0][1] = 128  # Saturation
        col[0][0][2] = 255  # Value
        cvcol = cv2.cvtColor(col, cv2.COLOR_HSV2BGR)
        col = (int(cvcol[0][0][0]), int(cvcol[0][0][1]), int(cvcol[0][0][2]))
        class_colors.append(col)

    vid = cv2.VideoCapture(0)

    # Compute aspect ratio of video
    vidw = vid.get(cv2.CAP_PROP_FRAME_WIDTH)
    vidh = vid.get(cv2.CAP_PROP_FRAME_HEIGHT)
    # vidar = vidw / vidh
    samples = os.listdir(save_path)
    sample_count = len(samples)
    empty_count = 0
    image_stack = []
    while True:
        retval, orig_image = vid.read()
        if not retval:
            print("Done!")
            return None

        im_size = (input_shape[0], input_shape[1])
        resized = cv2.resize(orig_image, im_size)
        rgb = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)

        inputs = [image.img_to_array(rgb)]
        tmp_inp = np.array(inputs)
        x = preprocess_input(tmp_inp)

        y = model.predict(x)

        results = bbox_util.detection_out(y)
        if len(results) > 0 and len(results[0]) > 0:
            det_label = results[0][:, 0]
            det_conf = results[0][:, 1]
            det_xmin = results[0][:, 2]
            det_ymin = results[0][:, 3]
            det_xmax = results[0][:, 4]
            det_ymax = results[0][:, 5]

            top_indices = [
                i for i, conf in enumerate(det_conf) if conf >= conf_thresh
            ]

            top_conf = det_conf[top_indices]
            top_label_indices = det_label[top_indices].tolist()
            top_xmin = det_xmin[top_indices]
            top_ymin = det_ymin[top_indices]
            top_xmax = det_xmax[top_indices]
            top_ymax = det_ymax[top_indices]

            if 15 not in top_label_indices:
                empty_count += 1
                if empty_count == 4:
                    image_stack = []
                    empty_count = 0
            else:
                empty_count = 0
                for i in range(top_conf.shape[0]):
                    xmin = int(round((top_xmin[i] * vidw) * 0.9))
                    ymin = int(round((top_ymin[i] * vidh) * 0.9))
                    xmax = int(round(
                        (top_xmax[i] * vidw) *
                        1.1)) if int(round(
                            (top_xmax[i] * vidw) * 1.1)) <= vidw else int(
                                round(top_xmax[i] * vidw))
                    ymax = int(round(
                        (top_ymax[i] * vidh) *
                        1.1)) if int(round(
                            (top_ymax[i] * vidh) * 1.1)) <= vidh else int(
                                round(top_ymax[i] * vidh))

                    # save frames
                    class_num = int(top_label_indices[i])
                    if class_num == 15:
                        cv2.rectangle(orig_image, (xmin, ymin), (xmax, ymax),
                                      class_colors[class_num], 2)
                        frame = orig_image
                        if len(image_stack) < frame_number:
                            image_stack.append(frame[ymin:ymax, xmin:xmax, :])
                        if len(image_stack) == frame_number:
                            image_stack.pop(0)
                            image_stack.append(frame[ymin:ymax, xmin:xmax, :])
        cv2.imshow("SSD result", orig_image)
        if cv2.waitKey(5) & 0xFF == ord('s'):
            if len(image_stack) == frame_number:
                if not os.path.exists(save_path + str(sample_count + 1)):
                    os.mkdir(save_path + str(sample_count + 1))
                for pic in range(frame_number):
                    cv2.imwrite(
                        save_path + str(sample_count + 1) + '/' +
                        str(1000 + pic) + '.jpg', image_stack[pic])
                    print('saving ' + save_path + str(sample_count + 1) + '/' +
                          str(1000 + pic) + '.jpg')
                image_stack = []
                empty_count = 0
                sample_count += 1
Beispiel #30
0
def dtc_predict_py_edit(
        predict_dir,
        predicted_dir,
        dict,
        model_path,
        conf_threshold=0.6,
        is_conf_threshold_down=False,
        class_model=None,
        dict_class={
            0.0: "Car",
            1.0: "Bicycle",
            2.0: "Pedestrian",
            3.0: "Signal",
            4.0: "Signs",
            5.0: "Truck"
        },
        img_height=331,
        img_width=331,
        is_overwrite=False,
        max_box=100  #None
    ,
        min_top_indices=0,
        fontsize=4,
        linewidth=0.5):
    """
    dtc_predict.py を一部変更した関数
    指定ディレクトリの画像1件ずつpredict実行し、バウンティングボックス付きの画像出力
    predictの位置や予測ラベルを書いたデータフレームも作成する
    Args:
        predict_dir : 予測したい画像がはいってるディレクトリ
        predicted_dir : 予測した画像出力先ディレクトリ
        dict : 予測クラスのidとクラス名の辞書型データ 例:dict = {0.0:"other", 1.0:"Bicycle", 2.0:"Pedestrian", 3.0:"Signal", 4.0:"Signs", 5.0:"Truck", 6.0:"Car"}
        model_path : ロードするモデルファイルのパス
        conf_threshold : 予測結果の確信度の閾値
        is_conf_threshold_down : 検出が出るまで予測結果の確信度の閾値を下げるかのフラグ
        class_model : 検出した領域をSSD以外のモデルで再予測する分類モデルオブジェクト
        dict_class : 再予測する分類モデルのクラスのidとクラス名の辞書型データ
        img_height, img_width : 再予測する分類モデルの入力画像サイズ(modelのデフォルトのサイズである必要あり)
        is_overwrite : 出力先に同名ファイルあればpredictしないかどうか
        max_box : 1画像で検出する領域の最大数。Noneなら制限なし。100なら100個まで検出
        min_top_indices : 最小でもmin_top_indices+1個は検出する。デフォルトの0なら最低1個は検出。is_conf_threshold_down=Trueでないと機能しない
        fontsize: 画像に表示する予測ラベルの文字の大きさ
        linewidth: 画像に表示する予測boxの線の太さ
    Return:
        なし(予測した画像出力、予測結果のデータフレーム出力(pred.csv))
    """
    num_classes = len(dict)  #6+1

    # 検出したとする確信度のしきい値
    #conf_threshold = 0.6#0.5#0.7

    # 予測する画像が入っているフォルダ
    #predict_dir = r'C:\Users\shingo\jupyter_notebook\tfgpu_py36_work\AI_Edge_Contest\object_detection\SSD_classes_py\all_SSD_module\SSD\ssd_train'
    # 予測する画像のパス一覧
    img_path_list = glob.glob(os.path.join(predict_dir, "*.*"))

    # 予測結果を保存するフォルダ
    ##predicted_dir = r'D:\work\AI_Edge_Contest\object_detect\object_detection\SSD_classes\predicted_images'
    if not os.path.isdir(predicted_dir):
        os.mkdir(predicted_dir)

    file_names = []  # ファイル名一覧
    inputs = []  # ネットワークへ入力するため指定サイズに変形済みの画像データ
    images_h = []  # オリジナルサイズの画像の縦幅
    images_w = []  # オリジナルサイズの画像の横幅
    images = []  # 結果を見るためのオリジナルサイズの画像データ
    correctpred_filecount = 0

    # メニュー辞書作成
    #dict = {0.0:"other", 1.0:"Bicycle", 2.0:"Pedestrian", 3.0:"Signal", 4.0:"Signs", 5.0:"Truck", 6.0:"Car"}

    # モデルロード
    model = create_model(num_classes)
    #model.load_weights(r'D:\work\AI_Edge_Contest\object_detect\object_detection\SSD_classes\weight_ssd_best.hdf5')
    model.load_weights(model_path)
    print(model)

    import pandas as pd
    # 空のデータフレーム作成
    pred_df = pd.DataFrame(
        index=[],
        columns=['file_names', 'conf', 'label_name', 'x', 'y', 'x+w', 'y+h'])

    # ---- json用 ----
    prediction = {}
    # ----------------

    # 画像情報1件ずつ取得
    for path in tqdm(img_path_list):

        # 出力先に同名ファイルあればpredictしない
        if is_overwrite == False and os.path.isfile(
                os.path.join(predicted_dir, os.path.basename(path))):
            continue

        file_names = []
        file_names.append(os.path.basename(path))
        #print(file_names)
        # ---- json用 ----
        img_name = os.path.basename(path)
        prediction[img_name] = {}
        # ----------------

        img, height, width = load_img(path, target_size=input_shape)
        img = image.img_to_array(img)

        inputs = []
        inputs.append(img.copy())

        images_h = []
        images_h.append(height)

        images_w = []
        images_w.append(width)

        images = []
        temp_image = imread(path)
        images.append(temp_image.copy())

        # 入力画像前処理
        inputs = preprocess_input(np.array(inputs))
        #print(inputs.shape)

        # 予測実行
        pred_results = model.predict(inputs, batch_size=1, verbose=0)
        #print(pred_results)
        bbox_util = BBoxUtility(num_classes)
        #print(bbox_util)
        bbox_results = bbox_util.detection_out(pred_results)
        #print(bbox_results)

        for file_no in range(len(file_names)):
            #for file_no in range(100):
            #print('-----------', file_names[file_no], '-----------')

            # 元の画像を描画
            plt.imshow(images[file_no] / 255.)

            # 予想したボックスの情報を取得
            bbox_label = bbox_results[file_no][:, 0]
            bbox_conf = bbox_results[file_no][:, 1]
            bbox_xmin = bbox_results[file_no][:, 2]
            bbox_ymin = bbox_results[file_no][:, 3]
            bbox_xmax = bbox_results[file_no][:, 4]
            bbox_ymax = bbox_results[file_no][:, 5]

            # 確信度がしきい値以上のボックスのみ抽出
            top_indices = [
                i for i, conf in enumerate(bbox_conf) if conf > conf_threshold
            ]

            # --------- len(top_indices) > min_top_indices になるまでconf_threshold 下げるか --------------------
            if is_conf_threshold_down == True:
                conf_threshold_change = 0.0
                if len(top_indices) == 0:
                    # 基準のconf_threshold で検出なければ、検出でるまで閾値下げる
                    for conf_threshold_i in range(int(conf_threshold // 0.01)):
                        conf_threshold_change = conf_threshold - (
                            (conf_threshold_i + 1) * 0.01)
                        top_indices = [
                            i for i, conf in enumerate(bbox_conf)
                            if conf > conf_threshold_change
                        ]
                        if len(top_indices) > min_top_indices:
                            #print('conf_threshold_i :', conf_threshold_i)
                            break
                            #continue
                #print('len(top_indices) :', len(top_indices))
                #print('conf_threshold_change :', conf_threshold_change)
            # -----------------------------------------------------------------------------------

            img_h = images_h[file_no]
            img_w = images_w[file_no]
            currentAxis = plt.gca()

            for box_no, top_index in enumerate(top_indices):
                # 検出数の最大値超えたらcontinue
                #( AI_Edge_Contest では1画像に100件までの制限あるため)
                if (max_box is not None) and (box_no >= max_box):
                    continue

                # 予想したボックスを作成
                label = bbox_label[top_index]
                #print('label:', label)
                x = int(bbox_xmin[top_index] * img_w)
                y = int(bbox_ymin[top_index] * img_h)
                w = int((bbox_xmax[top_index] - bbox_xmin[top_index]) * img_w)
                h = int((bbox_ymax[top_index] - bbox_ymin[top_index]) * img_h)
                box = (x, y), w, h

                # 予想したボックスを描画
                conf = float(bbox_conf[top_index])
                label_name = dict[label]
                # -------------------- 分類モデルで予測 --------------------
                # 検出に加えるかのフラグ
                is_inclode = True
                if class_model is not None:
                    if conf < conf_threshold:
                        # (ndarray型の画像データから)検出領域切り出し
                        # ndarray型の切り出しは[y:y_max,x:x_max]の順番じゃないとおかしくなる
                        # https://qiita.com/tadOne/items/8967f046ca395669329d
                        tmp_img = images[file_no]
                        dst = tmp_img[y:y + h, x:x + w]
                        # ここで画像表示すると、bbox付き画像保存されない.あくまで確認用
                        #plt.imshow(dst / 255.)
                        #plt.show()

                        #print('file_names :', file_names[file_no])
                        #print('label_name :', label_name)
                        #print('conf :', conf)
                        # 切り出し画像を分類モデルでpredict
                        class_conf, class_label_id = predict_class_model(
                            dst, class_model, img_height, img_width)
                        #print('class_label_name :', dict_class[class_label_id])
                        #print('class_conf :', class_conf)

                        # 分類モデルの方がスコア高ければ、ラベルとスコア書き換える
                        if conf <= class_conf:
                            label_name = dict_class[class_label_id]
                            conf = float(class_conf)
                        #elif top_index > 1:
                        #    # 検出数が1以上あってスコア低ければ検出に加えない
                        #    is_inclode = False
                # ---------------------------------------------------------

                # スコア低ければ検出に加えない
                if is_inclode == True:
                    # 画像にbbox描画
                    display_txt = '{:0.2f}, {}'.format(conf, label_name)
                    currentAxis.add_patch(
                        plt.Rectangle(*box,
                                      fill=False,
                                      edgecolor=get_class_color(label),
                                      linewidth=linewidth))
                    currentAxis.text(x,
                                     y,
                                     display_txt,
                                     bbox={
                                         'facecolor': get_class_color(label),
                                         'alpha': 0.2
                                     },
                                     fontsize=fontsize)
                    # 結果をデータフレームで保持
                    series = pd.Series([
                        file_names[file_no], conf, label_name, x, y, x + w,
                        y + h
                    ],
                                       index=pred_df.columns)
                    #print(series)
                    pred_df = pred_df.append(series, ignore_index=True)
                    #print(pred_df)
                    # -------------------------- json用 --------------------------
                    if label_name not in prediction[img_name]:
                        prediction[img_name][label_name] = []
                    prediction[img_name][label_name].append(
                        [x, y, x + w, y + h])
                    #print(prediction)
                    # ------------------------------------------------------------

            # 予測結果の画像ファイルを保存
            plt.savefig(os.path.join(predicted_dir, file_names[file_no]),
                        dpi=300)
            plt.clf()

    output_dir = os.path.dirname(predicted_dir)
    pred_df.to_csv(os.path.join(output_dir, 'pred.csv'), sep='\t', index=False)

    # -------------------------- json用 --------------------------
    with open(os.path.join(output_dir, 'pred.json'), 'w') as f:
        json.dump(prediction, f, indent=4)  # インデント付けてjsonファイル出力