Esempio n. 1
0
def main(args):
    base_model = gen_base_model()
    models, image_lists, label_lists = {}, {}, {}
    with open('answer.csv', 'w') as answer_file:
        answer_writer = csv.writer(answer_file, lineterminator='\n')
        with open(args.csv_file, 'r') as csvfile:
            csv_reader = csv.reader(csvfile)
            for [jpg_file, label_category, label] in csv_reader:
                weights_file = os.path.join(args.retrain_base_dir,
                                            label_category, args.weights)
                if os.path.exists(weights_file) and label == '?':
                    print('\ncategory label: ', label_category)
                    if label_category not in models:
                        image_lists_path = os.path.join(
                            args.retrain_base_dir, label_category,
                            'image_lists.json')
                        assert os.path.exists(image_lists_path)
                        with open(image_lists_path, 'r') as f:
                            image_lists[label_category] = json.load(f)
                            n_classes = len(image_lists[label_category].keys())
                            label_lists[label_category] = \
                                sorted(
                                    [i for i in image_lists[label_category].keys()])
                            if label_lists[label_category][0] == 'label 0':
                                # There are some data that never behave as 'label 0'
                                # and our model doesn't predict
                                # but we still need to write its prob as 0 later
                                need_label_0 = False
                            else:
                                need_label_0 = True
                        models[label_category] = make_model(
                            base_model, add_final_layer, weights_file,
                            n_classes)
                    # write answers, eg. '0.4365;0.5180;0.8741;0.2189;0.3938;0.0422'
                    img = image.load_img(os.path.join(args.rank_dir, jpg_file))
                    pred = predict_from_img(models[label_category], img)
                    pred_ind = pred.argmax(axis=-1)
                    print('prediction for {}\n\tis {}. '.format(
                        jpg_file.split('/')[-1], pred_ind + int(need_label_0)),
                          end='')
                    print('label:', label_lists[label_category][pred_ind])
                    label_pred = [0 for _ in range(int(need_label_0))]
                    label_pred.extend(pred)
                    label = ';'.join(
                        ['{:.5f}'.format(prob) for prob in label_pred])
                    print('output:', label)
                answer_writer.writerow([jpg_file, label_category, label])
Esempio n. 2
0
 def bottle_pred_func(img):
     # target_size = (IM_WIDTH, IM_HEIGHT)
     # img = image.load_img(file, target_size=target_size)
     return predict_from_img(base_model, img)
Esempio n. 3
0
def main(args):
    """Use transfer learning and fine-tuning to train a network on a new dataset"""

    base_model = None

    image_lists = load_training_data(args)
    n_classes = len(image_lists.keys())
    weights_file = os.path.join(args.model_dir, args.weights)
    if not os.path.exists(weights_file):
        print('weights_file: {} not found'.format(weights_file))
        raise FileNotFoundError
    bottleneck_dir = os.path.join(args.model_dir, 'distorted_bottlenecks/')

    if args.make_new_model:
        # use bottleneck, here the model must be identical to the original top layer
        # print(base_model.output.shape)
        target_size = (IM_WIDTH, IM_HEIGHT)
        base_model = gen_base_model()
        model = make_model(base_model, add_final_layer, weights_file,
                           n_classes)
        for category in ['testing', 'validation', 'training']:
            print('\ncategory:', category)
            img_list = []
            truths_list = []
            for label_index, label_name in enumerate(image_lists.keys()):
                for image_index, image_name in enumerate(
                        image_lists[label_name][category]):
                    image_name = get_image_path(image_lists, label_name,
                                                image_index, args.image_dir,
                                                category)
                    img_list.append(
                        image.load_img(image_name, target_size=target_size))
                    truths_list.append(label_index)
            preds = predict_from_img(model, img_list)
            pred_classes = preds.argmax(axis=-1)
            ind = np.nonzero(np.array(pred_classes) != np.array(truths_list))
            print('truths:', np.array(truths_list)[ind])
            print('preds:', pred_classes[ind], preds[ind])
            # img = image.load_img(file, target_size=target_size)
    # model.save(os.path.join(args.model_dir, 'inceptionv3-ft.model'))
    else:
        retrain_input_tensor = Input(shape=(BOTTLENECK_DIM, ))
        retrain_model = add_final_layer(retrain_input_tensor,
                                        retrain_input_tensor, n_classes)
        print('loading weights: {}'.format(weights_file))
        retrain_model.load_weights(weights_file)
        compile_retrain_model(retrain_model)
        base_model = None

        def bottle_pred_func(img):
            # target_size = (IM_WIDTH, IM_HEIGHT)
            # img = image.load_img(file, target_size=target_size)
            return predict_from_img(base_model, img)

        for category in ['testing', 'validation', 'training']:
            print('\n{} data:'.format(category))
            (test_x, test_y, files) = get_random_cached_bottlenecks(
                image_lists, -1, category, bottleneck_dir, args.image_dir,
                bottle_pred_func)
            print(retrain_model.test_on_batch(test_x, test_y))
            preds = retrain_model.predict_on_batch(np.array(test_x))
            pred_classes = preds.argmax(axis=-1)
            ind = np.nonzero(np.array(pred_classes) != np.array(test_y))
            print('truths:', test_y[ind])
            print('preds:', pred_classes[ind], preds[ind])