Example #1
0
    def decode(self, code):
        with tf.variable_scope('unpool1'):
            unpool1 = self.unpool(code)
            deconv1 = self.deconv(unpool1, [512, 512], 'deconv5_3')
            deconv2 = self.deconv(deconv1, [512, 512], 'deconv5_2')
            deconv3 = self.deconv(deconv2, [512, 512], 'deconv5_1')

        with tf.variable_scope('unpool2'):
            unpool2 = self.unpool(deconv3)
            deconv4 = self.deconv(unpool2, [512, 512], 'deconv4_3')
            deconv5 = self.deconv(deconv4, [512, 512], 'deconv4_2')
            deconv6 = self.deconv(deconv5, [256, 512], 'deconv4_1')

        with tf.variable_scope('unpool3'):
            unpool3 = self.unpool(deconv6)
            deconv7 = self.deconv(unpool3, [256, 256], 'deconv3_3')
            deconv8 = self.deconv(deconv7, [256, 256], 'deconv3_2')
            deconv9 = self.deconv(deconv8, [128, 256], 'deconv3_1')

        with tf.variable_scope('unpool4'):
            unpool4 = self.unpool(deconv9)
            deconv10 = self.deconv(unpool4, [128, 128], 'deconv2_2')
            deconv11 = self.deconv(deconv10, [64, 128], 'deconv2_1')

        with tf.variable_scope('unpool5'):
            unpool5 = self.unpool(deconv11)
            deconv12 = self.deconv(unpool5, [64, 64], 'deconv1_2')
            deconv13 = self.deconv(deconv12, [self.n, 64], 'deconv1_1')

        rgb_image = classifier.rgb(deconv13)
        tf.summary.image('output', rgb_image, max_outputs=self.max_images)
        return deconv13
    def strided_inference(self, images):
        tf.summary.image('input', images, max_outputs=self.max_images)

        with tf.variable_scope('encode1'):
            conv1 = self.conv(images, [3, 64], 'conv1_1')
            conv2 = self.conv2(conv1, [64, 64], 'conv1_2')

        with tf.variable_scope('encode2'):
            conv3 = self.conv(conv2, [64, 128], 'conv2_1')
            conv4 = self.conv2(conv3, [128, 128], 'conv2_2')

        with tf.variable_scope('encode3'):
            conv5 = self.conv(conv4, [128, 256], 'conv3_1')
            conv6 = self.conv(conv5, [256, 256], 'conv3_2')
            conv7 = self.conv2(conv6, [256, 256], 'conv3_3')

        with tf.variable_scope('decode1'):
            deconv7 = self.resize_conv(conv7, [256, 256], 'deconv3_3')
            deconv6 = self.deconv(deconv7, [256, 256], 'deconv3_2')
            deconv5 = self.deconv(deconv6, [128, 256], 'deconv3_1')

        with tf.variable_scope('decode2'):
            deconv4 = self.resize_conv(deconv5, [128, 128], 'deconv2_2')
            deconv3 = self.deconv(deconv4, [64, 128], 'deconv2_1')

        with tf.variable_scope('decode3'):
            deconv2 = self.resize_conv(deconv3, [64, 64], 'deconv1_2')
            deconv1 = self.deconv(deconv2, [self.n, 64], 'deconv1_1')

        rgb_image = classifier.rgb(deconv1)
        tf.summary.image('output', rgb_image, max_outputs=self.max_images)
        return deconv1
Example #3
0
    def strided_inference(self, images):
        tf.summary.image('input', images, max_outputs=self.max_images)

        with tf.variable_scope('pool1'):
            conv1 = self.conv(images, [3, 64], 'conv1_1')
            conv2 = self.conv2(conv1, [64, 64], 'conv1_2')

        with tf.variable_scope('pool2'):
            conv3 = self.conv(conv2, [64, 128], 'conv2_1')
            conv4 = self.conv2(conv3, [128, 128], 'conv2_2')

        with tf.variable_scope('pool3'):
            conv5 = self.conv(conv4, [128, 256], 'conv3_1')
            conv6 = self.conv(conv5, [256, 256], 'conv3_2')
            conv7 = self.conv2(conv6, [256, 256], 'conv3_3')

        with tf.variable_scope('pool4'):
            conv8 = self.conv(conv7, [256, 512], 'conv4_1')
            conv9 = self.conv(conv8, [512, 512], 'conv4_2')
            conv10 = self.conv2(conv9, [512, 512], 'conv4_3')

        # with tf.variable_scope('pool5'):
        #   conv11 = self.conv(conv10, [512, 512], 'conv5_1')
        #   conv12 = self.conv(conv11, [512, 512], 'conv5_2')
        #   conv13 = self.conv2(conv12, [512, 512], 'conv5_3')

        with tf.variable_scope('unpool1'):
            # deconv1 = self.resize_conv(conv13, [512, 512], 'deconv5_3')
            deconv1 = self.resize_conv(conv10, [512, 512], 'deconv5_3')
            deconv2 = self.deconv(deconv1, [512, 512], 'deconv5_2')
            deconv3 = self.deconv(deconv2, [512, 512], 'deconv5_1')

        with tf.variable_scope('unpool2'):
            deconv4 = self.resize_conv(deconv3, [512, 512], 'deconv4_3')
            deconv5 = self.deconv(deconv4, [512, 512], 'deconv4_2')
            deconv6 = self.deconv(deconv5, [256, 512], 'deconv4_1')

        with tf.variable_scope('unpool3'):
            deconv7 = self.resize_conv(deconv6, [256, 256], 'deconv3_3')
            deconv8 = self.deconv(deconv7, [256, 256], 'deconv3_2')
            deconv9 = self.deconv(deconv8, [128, 256], 'deconv3_1')

        with tf.variable_scope('unpool4'):
            # deconv10 = self.resize_conv(deconv9, [128, 128], 'deconv2_2')
            deconv11 = self.deconv(deconv9, [64, 128], 'deconv2_1')

        with tf.variable_scope('unpool5'):
            deconv12 = self.resize_conv(deconv11, [64, 64], 'deconv1_2')
            deconv13 = self.deconv(deconv12, [self.n, 64], 'deconv1_1')

        rgb_image = classifier.rgb(deconv13)
        print rgb_image
        tf.summary.image('output', rgb_image, max_outputs=self.max_images)
        return deconv13
Example #4
0
def test():
    if config.working_dataset == 'UBC_easy' or config.working_dataset == 'forced_UBC_easy' \
    or config.working_dataset == 'UBC_interpolated' or config.working_dataset == 'UBC_hard':

        if config.working_dataset == 'UBC_easy' or config.working_dataset == 'UBC_hard':
            gt_folder = 'groundtruth'
        elif config.working_dataset == 'forced_UBC_easy':
            gt_folder = 'forced_groundtruth'
        elif config.working_dataset == 'UBC_interpolated':
            gt_folder = 'interpol_groundtruth'

        images = tf.placeholder(tf.float32, shape=[224, 224, 4])
        image = tf.slice(images, [0, 0, 0], [224, 224, 3])
        alpha = tf.slice(images, [0, 0, 2], [224, 224, 1])
        alpha = tf.cast(alpha * 255, tf.uint8)
        image /= 255
        image = tf.reshape(image, [-1, 224, 224, 3])

        labels = tf.placeholder(tf.float32, shape=[224, 224, 4])
        label = tf.slice(labels, [0, 0, 0], [224, 224, 3])
        label /= 255
        label = tf.reshape(label, [-1, 224, 224, 3])

        tf.summary.image('labels', label)
        one_hot_labels = classifier.one_hot(label)

        autoencoder = utils.get_autoencoder(config.autoencoder,
                                            config.working_dataset,
                                            config.strided)
        logits = autoencoder.inference(image)

        rgb_image = classifier.rgb(logits)
        tf.summary.image('output', rgb_image, max_outputs=3)

        rgb_image = tf.reshape(tf.cast(rgb_image * 255, tf.uint8),
                               [224, 224, 3])
        rgba_image = tf.concat([rgb_image, alpha], 2)

        # Calculate Accuracy
        accuracy_op = accuracy(logits, one_hot_labels)
        tf.summary.scalar('accuracy', accuracy_op)

        pc_accuracy = per_class_accuracy(logits, one_hot_labels)
        pc_size = pc_accuracy.get_shape().as_list()
        for k in range(pc_size[0]):
            tf.summary.scalar('accuracy_class%02d' % (k + 1), pc_accuracy[k])
        tf.summary.tensor_summary('class_wise_accuracy', pc_accuracy)

        saver = tf.train.Saver(tf.global_variables())

        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=config.gpu_memory_fraction)
        session_config = tf.ConfigProto(allow_soft_placement=True,
                                        gpu_options=gpu_options)

        with tf.Session(config=session_config) as sess:
            # File paths
            test_logs, ckpt_dir, main_dir, main_output_dir = config.get_directories(
                config.working_dataset)

            # Store Summaries
            summary = tf.summary.merge_all()
            summary_writer = tf.summary.FileWriter(test_logs)
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)

            ckpt = tf.train.get_checkpoint_state(ckpt_dir)

            if not (ckpt and ckpt.model_checkpoint_path):
                print('No checkpoint file found')
                return

            ckpt_path = ckpt.model_checkpoint_path
            saver.restore(sess, ckpt_path)

            # Start Testing on the set
            train_set = 20
            cam_range = 3

            for num_test in range(1, train_set + 1):
                cwa = np.array([])
                for num_cam in range(1, cam_range + 1):
                    print 'Currently processing subject numbeer ' + str(
                        num_test) + ' camera no- ' + str(num_cam)

                    main_input_dir = '%s%d/images' % (main_dir, num_test)
                    depth_file_list = glob.glob(
                        os.path.join(main_input_dir, 'depthRender',
                                     'Cam%d' % num_cam, '*.png'))
                    label_file_list = glob.glob(
                        os.path.join(main_input_dir, gt_folder,
                                     'Cam%d' % num_cam, '*.png'))
                    output_dir = '%s%d/Cam%d' % (main_output_dir, num_test,
                                                 num_cam)
                    if not os.path.exists(output_dir):
                        os.makedirs(output_dir)

                    for depth_file_name, label_file_name in zip(
                            depth_file_list, label_file_list):
                        output_file_name = os.path.join(
                            output_dir,
                            depth_file_name.split('/')[-1])
                        # print output_file_name
                        if not os.path.exists(output_file_name):
                            depth_image = Image.open(depth_file_name)
                            label_image = Image.open(label_file_name)

                            # Find Min-max non zero pixels in the label image
                            label_image_array = np.array(label_image).sum(
                                axis=2)

                            itemidx = np.where(
                                label_image_array.sum(axis=0) != 0)
                            itemidy = np.where(
                                label_image_array.sum(axis=1) != 0)

                            # Crop and Resize Test Depth Image
                            cropped_depth_image = depth_image.crop(
                                (min(itemidx[0]), min(itemidy[0]),
                                 max(itemidx[0]), max(itemidy[0])))
                            cropped_depth_image = cropped_depth_image.resize(
                                (224, 224), Image.NEAREST)

                            # Crop and Resize Test Label Image
                            cropped_label_image = label_image.crop(
                                (min(itemidx[0]), min(itemidy[0]),
                                 max(itemidx[0]), max(itemidy[0])))
                            cropped_label_image = cropped_label_image.resize(
                                (224, 224), Image.NEAREST)

                            # Infer body-part labels from the learned model
                            summary_str, inferred_image, pc_acc = sess.run(
                                [summary, rgba_image, pc_accuracy],
                                feed_dict={
                                    images: cropped_depth_image,
                                    labels: cropped_label_image
                                })

                            cwa = np.concatenate(
                                [cwa, np.expand_dims(pc_acc, 0)],
                                axis=0) if cwa.size else np.expand_dims(
                                    pc_acc, 0)
                            # Restore the original size of the inferred label image
                            inferred_image = Image.fromarray(
                                inferred_image.astype('uint8'))

                            # Reshape to original size and aspect ratio
                            resized_inferred_image = inferred_image.resize(
                                (max(itemidx[0]) - min(itemidx[0]),
                                 max(itemidy[0]) - min(itemidy[0])),
                                Image.NEAREST)
                            resized_inferred_image = np.array(
                                resized_inferred_image)
                            Pad = np.zeros((424, 512, 4))
                            Pad[min(itemidy[0]):max(itemidy[0]),
                                min(itemidx[0]):max(
                                    itemidx[0]), :] = resized_inferred_image
                            resized_inferred_image = Image.fromarray(
                                Pad.astype('uint8'))

                            # Save the restored image
                            resized_inferred_image.save(output_file_name)

                            # Write the summary collected from the session
                            summary_writer.add_summary(summary_str)
                            summary_writer.flush()

                sub_dir = '%s%d/' % (main_output_dir, num_test)
                if not config.working_dataset == 'MHAD_UBC':
                    hdf5storage.write(np.transpose(cwa),
                                      path='/cwa',
                                      filename=sub_dir +
                                      'class_wise_accuracy.mat',
                                      matlab_compatible=True)
            coord.request_stop()
            coord.join(threads)

    elif config.working_dataset == 'MHAD' or config.working_dataset == 'UBC_MHAD':
        images = tf.placeholder(tf.float32, shape=[224, 224, 3])
        image = tf.slice(images, [0, 0, 0], [224, 224, 3])
        image /= 255
        image = tf.reshape(image, [-1, 224, 224, 3])

        labels = tf.placeholder(tf.float32, shape=[224, 224, 3])
        label = tf.slice(labels, [0, 0, 0], [224, 224, 3])
        label /= 255
        label = tf.reshape(label, [-1, 224, 224, 3])

        tf.summary.image('labels', label)

        autoencoder = utils.get_autoencoder(config.autoencoder,
                                            config.working_dataset,
                                            config.strided)
        logits = autoencoder.inference(image)

        rgb_image = classifier.rgb(logits)
        tf.summary.image('output', rgb_image, max_outputs=3)

        one_hot_labels = classifier.one_hot(label)

        rgb_image = tf.reshape(tf.cast(rgb_image * 255, tf.uint8),
                               [224, 224, 3])

        pc_accuracy = per_class_accuracy(logits, one_hot_labels)
        pc_size = pc_accuracy.get_shape().as_list()
        for k in range(pc_size[0]):
            tf.summary.scalar('accuracy_class%02d' % (k + 1), pc_accuracy[k])
        tf.summary.tensor_summary('class_wise_accuracy', pc_accuracy)

        saver = tf.train.Saver(tf.global_variables())
        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=config.gpu_memory_fraction)
        session_config = tf.ConfigProto(allow_soft_placement=True,
                                        gpu_options=gpu_options)
        with tf.Session(config=session_config) as sess:
            if FLAGS.random_split_mode:
                # File paths
                test_logs, ckpt_dir, main_input_dir, output_sub_dir = config.get_directories(
                    config.working_dataset)
            else:
                test_logs, ckpt_dir, main_input_dir, output_sub_dir = config.get_directories(
                    config.working_dataset)
                test_logs = os.path.join(test_logs, str(
                    FLAGS.test_subject)) + '/'
                ckpt_dir = os.path.join(ckpt_dir, str(
                    FLAGS.test_subject)) + '/'
                output_sub_dir = os.path.join(output_sub_dir,
                                              str(FLAGS.test_subject)) + '/'

            # Store Summaries and Initialize threads
            summary = tf.summary.merge_all()
            summary_writer = tf.summary.FileWriter(test_logs)
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)

            ckpt = tf.train.get_checkpoint_state(ckpt_dir)

            if not (ckpt and ckpt.model_checkpoint_path):
                print('No checkpoint file found')
                return

            ckpt_path = ckpt.model_checkpoint_path
            saver.restore(sess, ckpt_path)
            subjects = 12
            cameras = 2
            actions = 11
            recordings = 5

            for cam in range(1, cameras + 1):
                for sub in range(1, subjects + 1):
                    for act in range(1, actions + 1):
                        cwa = np.array([])
                        for rec in range(1, recordings + 1):
                            print 'Currently processing subject no- ' + str(
                                sub) + ' action no- ' + str(
                                    act) + ' recording no = ' + str(rec)
                            depth_file_list = glob.glob(
                                os.path.join(
                                    main_input_dir,
                                    'Kin%02d/S%02d/A%02d/R%02d/depth_png' %
                                    (cam, sub, act, rec), '*.png'))

                            label_file_list = glob.glob(
                                os.path.join(
                                    main_input_dir,
                                    'Kin%02d/S%02d/A%02d/R%02d/labels' %
                                    (cam, sub, act, rec), '*.png'))

                            output_dir = os.path.join(
                                main_input_dir, output_sub_dir,
                                'Kin%02d/S%02d/A%02d/R%02d/' %
                                (cam, sub, act, rec))
                            if not os.path.exists(output_dir):
                                os.makedirs(output_dir)
                            for depth_file_name, label_file_name in zip(
                                    depth_file_list, label_file_list):
                                output_file_name = os.path.join(
                                    output_dir,
                                    depth_file_name.split('/')[-1])

                                if not os.path.exists(output_file_name):
                                    depth_image = cv.imread(depth_file_name)
                                    label_image = cv.imread(label_file_name)
                                    depth_image_array = np.array(depth_image)
                                    label_image_array = np.array(label_image)

                                    # Find Min-max non zero pixels in the label image
                                    itemidx = np.where(
                                        depth_image_array.sum(axis=0) != 0)
                                    itemidy = np.where(
                                        depth_image_array.sum(axis=1) != 0)
                                    # print itemidx

                                    # Crop and Resize Test Depth Image
                                    try:
                                        cropped_image = depth_image_array[
                                            min(itemidy[0]):max(itemidy[0]),
                                            min(itemidx[0]):max(itemidx[0]), :]
                                        resized_depth_image = cv.resize(
                                            cropped_image, (224, 224),
                                            interpolation=cv.INTER_LINEAR)

                                        cropped_label_image = label_image_array[
                                            min(itemidy[0]):max(itemidy[0]),
                                            min(itemidx[0]):max(itemidx[0]), :]
                                        resized_label_image = cv.resize(
                                            cropped_label_image, (224, 224),
                                            interpolation=cv.INTER_LINEAR)

                                        # Infer body-part labels from the learned model
                                        summary_str, inferred_image, pc_acc = sess.run(
                                            [summary, rgb_image, pc_accuracy],
                                            feed_dict={
                                                images: resized_depth_image,
                                                labels: resized_label_image
                                            })

                                        cwa = np.concatenate(
                                            [cwa,
                                             np.expand_dims(pc_acc, 0)],
                                            axis=0
                                        ) if cwa.size else np.expand_dims(
                                            pc_acc, 0)

                                        # Reshape to original size and aspect ratio
                                        resized_inferred_image = cv.resize(
                                            inferred_image,
                                            ((max(itemidx[0]) -
                                              min(itemidx[0])),
                                             (max(itemidy[0]) -
                                              min(itemidy[0]))),
                                            interpolation=cv.INTER_NEAREST)

                                        resized_inferred_image = np.array(
                                            resized_inferred_image)
                                        Pad = np.zeros((480, 640, 3))
                                        Pad[min(itemidy[0]):max(itemidy[0]),
                                            min(itemidx[0]):
                                            max(itemidx[0]
                                                ), :] = resized_inferred_image
                                        cv.imwrite(output_file_name,
                                                   Pad[..., ::-1])

                                        # Write the summary collected from the session
                                        summary_writer.add_summary(summary_str)
                                        summary_writer.flush()

                                    except:
                                        print 'Skipping the image name %s' % depth_file_name

                            sub_dir = os.path.join(
                                main_input_dir, output_sub_dir,
                                'Kin%02d/S%02d/A%02d/R%02d_cwa/' %
                                (cam, sub, act, rec))
                            if not os.path.exists(sub_dir):
                                os.makedirs(sub_dir)
                            hdf5storage.write(np.transpose(cwa),
                                              path='/cwa',
                                              filename=sub_dir +
                                              'class_wise_accuracy.mat',
                                              matlab_compatible=True)
            coord.request_stop()
            coord.join(threads)

    elif config.working_dataset == 'MHAD_UBC':

        images = tf.placeholder(tf.float32, shape=[224, 224, 4])
        image = tf.slice(images, [0, 0, 0], [224, 224, 3])
        alpha = tf.slice(images, [0, 0, 2], [224, 224, 1])
        alpha = tf.cast(alpha * 255, tf.uint8)
        image /= 255
        image = tf.reshape(image, [-1, 224, 224, 3])

        autoencoder = utils.get_autoencoder(config.autoencoder,
                                            config.working_dataset,
                                            config.strided)
        logits = autoencoder.inference(image)

        rgb_image = classifier.rgb(logits)
        tf.summary.image('output', rgb_image, max_outputs=3)

        rgb_image = tf.reshape(tf.cast(rgb_image * 255, tf.uint8),
                               [224, 224, 3])
        rgba_image = tf.concat([rgb_image, alpha], 2)

        saver = tf.train.Saver(tf.global_variables())

        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=config.gpu_memory_fraction)
        session_config = tf.ConfigProto(allow_soft_placement=True,
                                        gpu_options=gpu_options)

        with tf.Session(config=session_config) as sess:
            # File paths
            test_logs, ckpt_dir, main_dir, main_output_dir = config.get_directories(
                config.working_dataset)

            # Store Summaries
            summary = tf.summary.merge_all()
            summary_writer = tf.summary.FileWriter(test_logs)
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)

            ckpt = tf.train.get_checkpoint_state(ckpt_dir)

            if not (ckpt and ckpt.model_checkpoint_path):
                print('No checkpoint file found')
                return

            ckpt_path = ckpt.model_checkpoint_path
            saver.restore(sess, ckpt_path)

            # Start Testing on the set
            train_set = 20
            cam_range = 3

            for num_test in range(1, train_set + 1):
                cwa = np.array([])
                for num_cam in range(1, cam_range + 1):
                    print 'Currently processing subject number ' + str(
                        num_test) + ' camera no- ' + str(num_cam)

                    main_input_dir = '%s%d/images' % (main_dir, num_test)
                    depth_file_list = glob.glob(
                        os.path.join(main_input_dir, 'depthRender',
                                     'Cam%d' % num_cam, '*.png'))

                    output_dir = '%s%d/Cam%d' % (main_output_dir, num_test,
                                                 num_cam)
                    if not os.path.exists(output_dir):
                        os.makedirs(output_dir)

                    for depth_file_name in depth_file_list:
                        output_file_name = os.path.join(
                            output_dir,
                            depth_file_name.split('/')[-1])

                        if not os.path.exists(output_file_name):
                            depth_image = Image.open(depth_file_name)

                            # Find Min-max non zero pixels in the label image
                            depth_image_array = np.array(depth_image).sum(
                                axis=2)

                            itemidx = np.where(
                                depth_image_array.sum(axis=0) != 0)
                            itemidy = np.where(
                                depth_image_array.sum(axis=1) != 0)

                            # Crop and Resize Test Depth Image
                            cropped_depth_image = depth_image.crop(
                                (min(itemidx[0]), min(itemidy[0]),
                                 max(itemidx[0]), max(itemidy[0])))
                            cropped_depth_image = cropped_depth_image.resize(
                                (224, 224), Image.NEAREST)

                            # Infer body-part labels from the learned model
                            summary_str, inferred_image = sess.run(
                                [summary, rgba_image],
                                feed_dict={images: cropped_depth_image})

                            # Restore the original size of the inferred label image
                            inferred_image = Image.fromarray(
                                inferred_image.astype('uint8'))

                            # Reshape to original size and aspect ratio
                            resized_inferred_image = inferred_image.resize(
                                (max(itemidx[0]) - min(itemidx[0]),
                                 max(itemidy[0]) - min(itemidy[0])),
                                Image.NEAREST)
                            resized_inferred_image = np.array(
                                resized_inferred_image)
                            Pad = np.zeros((424, 512, 4))
                            Pad[min(itemidy[0]):max(itemidy[0]),
                                min(itemidx[0]):max(
                                    itemidx[0]), :] = resized_inferred_image
                            resized_inferred_image = Image.fromarray(
                                Pad.astype('uint8'))

                            # Save the restored image
                            resized_inferred_image.save(output_file_name)

                            # Write the summary collected from the session
                            summary_writer.add_summary(summary_str)
                            summary_writer.flush()

                sub_dir = '%s%d/' % (main_output_dir, num_test)
                if not config.working_dataset == 'MHAD_UBC':
                    hdf5storage.write(np.transpose(cwa),
                                      path='/cwa',
                                      filename=sub_dir +
                                      'class_wise_accuracy.mat',
                                      matlab_compatible=True)
            coord.request_stop()
            coord.join(threads)
Example #5
0
def train(train_filename, train_ckpt, train_logs, ckpt_dir):
    images, labels = inputs(FLAGS.batch, train_filename)
    tf.summary.image('labels', labels)
    one_hot_labels = classifier.one_hot(labels)

    autoencoder = utils.get_autoencoder(config.autoencoder,
                                        config.working_dataset, config.strided)
    logits = autoencoder.inference(images)

    # Store output images
    rgb_image = classifier.rgb(logits)
    tf.summary.image('output', rgb_image, max_outputs=3)

    # Loss/Accuracy Metrics
    accuracy_op = accuracy(logits, one_hot_labels)
    loss_op = loss(logits, one_hot_labels)

    pc_accuracy = per_class_accuracy(logits, one_hot_labels)
    pc_size = pc_accuracy.get_shape().as_list()
    for k in range(pc_size[0]):
        tf.summary.scalar('accuracy_class%02d' % (k + 1), pc_accuracy[k])
    tf.summary.tensor_summary('class_wise_accuracy', pc_accuracy)
    tf.summary.scalar('accuracy', accuracy_op)
    tf.summary.scalar(loss_op.op.name, loss_op)
    optimizer = tf.train.AdamOptimizer(1e-04)
    train_step = optimizer.minimize(loss_op)
    init = tf.global_variables_initializer()
    saver = tf.train.Saver()
    gpu_options = tf.GPUOptions(
        per_process_gpu_memory_fraction=config.gpu_memory_fraction)
    session_config = tf.ConfigProto(allow_soft_placement=True,
                                    gpu_options=gpu_options)

    with tf.Session(config=session_config) as sess:
        ckpt = tf.train.get_checkpoint_state(ckpt_dir)
        if not ckpt:
            print('No checkpoint file present. Initializing...')
            global_step = 0
            sess.run(init)

        else:
            saver.restore(sess, tf.train.latest_checkpoint(ckpt_dir))
            global_step = int(
                tf.train.latest_checkpoint(ckpt_dir).split('/')[-1].split('-')
                [-1])
            print 'Restoring the training from step- ' + str(global_step)

        summary = tf.summary.merge_all()
        summary_writer = tf.summary.FileWriter(train_logs, sess.graph)

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        for step in tqdm(range(global_step, FLAGS.steps + 1)):
            sess.run(train_step)

            if step % FLAGS.summary_step == 0:
                summary_str = sess.run(summary)
                summary_writer.add_summary(summary_str, step)
                summary_writer.flush()

                print '\ntraining step ' + str(step)
                print('\nAccuracy=  %06f , Loss=   %06f' %
                      (accuracy_op.eval(), loss_op.eval()))

            if step == 0:
                saver.save(sess, train_ckpt, global_step=step)
            elif step == 100 or step == 500 or step == 1000 or step == 5000 or step == 7500 or step == 10000 \
                or step == 15000 or step == FLAGS.steps:
                saver.save(sess,
                           train_ckpt,
                           global_step=step,
                           write_meta_graph=False)
        coord.request_stop()
        coord.join(threads)
    def inference_with_pooling(self, images):
        tf.summary.image('input', images, max_outputs=self.max_images)

        with tf.variable_scope('pool1'):
            conv1 = self.conv(images, [3, 64], 'conv1_1')
            conv2 = self.conv(conv1, [64, 64], 'conv1_2')
            pool1 = self.pool(conv2)

        with tf.variable_scope('pool2'):
            conv3 = self.conv(pool1, [64, 128], 'conv2_1')
            conv4 = self.conv(conv3, [128, 128], 'conv2_2')
            pool2 = self.pool(conv4)

        with tf.variable_scope('pool3'):
            conv5 = self.conv(pool2, [128, 256], 'conv3_1')
            conv6 = self.conv(conv5, [256, 256], 'conv3_2')
            conv7 = self.conv(conv6, [256, 256], 'conv3_3')
            pool3 = self.pool(conv7)

        with tf.variable_scope('pool4'):
            conv8 = self.conv(pool3, [256, 512], 'conv4_1')
            conv9 = self.conv(conv8, [512, 512], 'conv4_2')
            conv10 = self.conv(conv9, [512, 512], 'conv4_3')
            pool4 = self.pool(conv10)

        with tf.variable_scope('pool5'):
            conv11 = self.conv(pool4, [512, 512], 'conv5_1')
            conv12 = self.conv(conv11, [512, 512], 'conv5_2')
            conv13 = self.conv(conv12, [512, 512], 'conv5_3')
            pool5 = self.pool(conv13)

        with tf.variable_scope('unpool1'):
            unpool1 = self.unpool(pool5)
            deconv1 = self.deconv(unpool1, [512, 512], 'deconv5_3')
            deconv2 = self.deconv(deconv1, [512, 512], 'deconv5_2')
            deconv3 = self.deconv(deconv2, [512, 512], 'deconv5_1')

        with tf.variable_scope('unpool2'):
            unpool2 = self.unpool(deconv3)
            deconv4 = self.deconv(unpool2, [512, 512], 'deconv4_3')
            deconv5 = self.deconv(deconv4, [512, 512], 'deconv4_2')
            deconv6 = self.deconv(deconv5, [256, 512], 'deconv4_1')

        with tf.variable_scope('unpool3'):
            unpool3 = self.unpool(deconv6)
            deconv7 = self.deconv(unpool3, [256, 256], 'deconv3_3')
            deconv8 = self.deconv(deconv7, [256, 256], 'deconv3_2')
            deconv9 = self.deconv(deconv8, [128, 256], 'deconv3_1')

        with tf.variable_scope('unpool4'):
            unpool4 = self.unpool(deconv9)
            deconv10 = self.deconv(unpool4, [128, 128], 'deconv2_2')
            deconv11 = self.deconv(deconv10, [64, 128], 'deconv2_1')

        with tf.variable_scope('unpool5'):
            unpool5 = self.unpool(deconv11)
            deconv12 = self.deconv(unpool5, [64, 64], 'deconv1_2')
            deconv13 = self.deconv(deconv12, [self.n, 64], 'deconv1_1')

        rgb_image = classifier.rgb(deconv13)
        tf.summary.image('output', rgb_image, max_outputs=self.max_images)
        return deconv13