Exemplo n.º 1
0
def build_model(config):
    labels = tf.placeholder(tf.float32, shape=[None, config.num_class], name="labels")
    global_step = tf.Variable(0, trainable=False)
    is_training = tf.placeholder(tf.bool, shape=(), name="is_training")
    default_last_conv_name = None
    if config.model_name in model_factory.networks_map.keys():
        if config.model_name[:6] == "nasnet":
            is_training = config.train
        model_f = model_factory.get_network_fn(config.model_name, config.num_class, weight_decay=config.weight_decay,
                                               is_training=is_training)

        if hasattr(model_f, 'default_last_conv_name'):
            default_last_conv_name = model_f.default_last_conv_name

        if not config.input_size:
            config.input_size = model_f.default_image_size
        inputs = tf.placeholder(tf.float32, shape=[None, config.input_size, config.input_size, config.num_channel],
                                name="inputs")
        tf.summary.image('input', inputs, config.num_input_summary)
        logits, end_points = model_f(inputs)

        if config.model_name[:6] == "resnet":
            logits = tf.reshape(logits, [-1, config.num_class])

    else:
        build_model_f = util.get_attr('model.%s' % config.model_name, "build_model")
        if not build_model_f:
            return None
        if not config.input_size:
            config.input_size = util.get_attr('model.%s' % config.model_name, "input_size")
            if not config.input_size:
                config.input_size = 224
        default_last_conv_name = util.get_attr('model.%s' % config.model_name, "default_last_conv_name")

        inputs = tf.placeholder(tf.float32, shape=[None, config.input_size, config.input_size, config.num_channel],
                                name="inputs")
        tf.summary.image('input', inputs, config.num_input_summary)
        model_result = build_model_f(inputs, config, is_training=is_training)
        if isinstance(model_result, tuple):
            logits = model_result[0]
            end_points = model_result[1]
        else:
            logits = model_result
            end_points = None

    if hasattr(config, 'loss_file'):
        loss_file = config.loss_file
    else:
        loss_file = "softmax_cross_entropy"

    loss_f = util.get_attr('model.loss.%s' % loss_file, "build_loss")
    ops = None
    if loss_f:
        ops = loss_f(logits, labels, global_step, config)

    return inputs, labels, logits, end_points, is_training, global_step, default_last_conv_name, ops
Exemplo n.º 2
0
def do_slim_model_with_loading_ckpt(model_name, logits_layer_name, last_conv_layer_name, ckpt_file, synset, num_classes, image_size, num_channel, filenames):
	result_imgs = list()
	result_classes = list()
	summary_names = list()

	### Load image
	imgs = load_images(filenames, image_size)

	### Define model
	inputs = tf.placeholder(tf.float32, shape=[None, image_size, image_size, num_channel], name="inputs")
	model_f = model_factory.get_network_fn(model_name, num_classes, weight_decay=0.00004, is_training=False)
	logits, end_points = model_f(inputs)

	with tf.Session() as sess:
		sess.run(tf.global_variables_initializer())

		### Load weight
		saver = tf.train.Saver()
		saver.restore(sess, ckpt_file)

		### Predict
		probs = sess.run(end_points[logits_layer_name], feed_dict={inputs: imgs})

		### Create CAM image
		grad_cam_plus_plus = GradCamPlusPlus(sess, end_points[logits_layer_name], end_points[last_conv_layer_name], inputs)
		cam_imgs, class_indices = grad_cam_plus_plus.create_cam_img(imgs, probs)

		for i, filename in enumerate(filenames):
			box_img = np.copy(imgs[i])
			filename = os.path.basename(filename)
			for j in range(GradCamPlusPlus.TOP3):
				### Overlay heatmap
				heapmap = grad_cam_plus_plus.convert_cam_2_heatmap(cam_imgs[i][j])
				overlay_img = grad_cam_plus_plus.overlay_heatmap(imgs[i], heapmap)
				result_imgs.append(overlay_img)
				summary_names.append('{}-Top{}'.format(filename, j))

				### Boxing
				color = [0, 0, 0]
				color[j] = 255
				box_img = grad_cam_plus_plus.draw_rectangle(box_img, cam_imgs[i][j], color)

				### Get label
				result_classes.append(synset[class_indices[i][j]])

			result_imgs.append(box_img)
			summary_names.append('{}-Boxing'.format(filename))

		### Write summary
		write_summary('log', summary_names, result_imgs, sess)

	return result_imgs, result_classes
Exemplo n.º 3
0
    def init_config(self):
        label_path = os.path.join(self.config.dataset_dir, "labels.txt")
        num_class = util.count_label(label_path)
        self.config.num_class = num_class

        if not self.config.num_class:
            raise Exception("Check the label file : %s" % label_path)

        if self.config.train:
            train_filenames = util.get_tfrecord_filenames(
                self.config.dataset_name, self.config.dataset_dir)
            if not train_filenames:
                raise Exception("There is no tfrecord files")
            num_train_sample = util.count_records(train_filenames)

            self.config.num_train_sample = num_train_sample

        if self.config.validation:
            validation_filenames = util.get_tfrecord_filenames(
                self.config.dataset_name, self.config.dataset_dir,
                'validation')
            if not validation_filenames:
                raise Exception("There is no tfrecord files")

            num_validation_sample = util.count_records(validation_filenames)

            self.config.num_validation_sample = num_validation_sample
        # tf_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)
        tf_config = tf.ConfigProto(allow_soft_placement=True)
        tf_config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=tf_config)

        # self.sess = tf.Session(config=tf_config)
        self.is_training = tf.placeholder(tf.bool,
                                          shape=(),
                                          name="is_training")
        from slim.model import model_factory as mf
        self.model_f = mf.get_network_fn(self.config.model_name,
                                         self.config.num_class,
                                         weight_decay=self.config.weight_decay,
                                         is_training=self.is_training)

        if not self.config.input_size:
            self.config.input_size = self.model_f.default_image_size