Пример #1
0
    def __init__(self):

        self.params_dict = {}
        conv_layers = []
        dense_layers = []

        self.hook = _Hook(self.params_dict, is_training=True)

        self.config = tf.ConfigProto()
        self.config.gpu_options.allow_growth = True

        conv_layers.append(ConvLayer(1, 8, 3, 1, "input_conv_layer"))
        conv_layers.append(MaxPoolingLayer(2, 2, "first_pooling_layer"))
        conv_layers.append(ConvLayer(8, 16, 5, 1, "second_conv_layer"))
        conv_layers.append(MaxPoolingLayer(2, 2, "second_pooling_layer"))
        conv_layers.append(ConvLayer(16, 16, 5, 1, "code"))
        conv_layers.append(
            FSConvLayer(16, 8, [100, 14, 14, 8], 5, 2, "first_fsconv_layer"))
        conv_layers.append(
            FSConvLayer(8,
                        1, [100, 28, 28, 1],
                        5,
                        2,
                        "second_fsconv_layer",
                        f=lambda x: x))

        self.conv_layers = conv_layers
        self.dense_layers = dense_layers
Пример #2
0
    def __init__(self):

        self.params_dict = {}
        conv_layers = []
        dense_layers = []

        self.hook = _Hook(self.params_dict, is_training=True)


        self.config = tf.ConfigProto()
        self.config.gpu_options.allow_growth = True

        conv_layers.append(ConvLayer(1, 32, 3, 1, "input_conv_layer"))
        conv_layers.append(MaxPoolingLayer(2, 2, "first_pooling_layer"))
        conv_layers.append(ConvLayer(32, 64, 5, 1, "second_conv_layer"))
        conv_layers.append(MaxPoolingLayer(2, 2, "second_pooling_layer"))
        dense_layers.append(DenseLayer(7*7*64, 1024, "first_dense_layer") ) #size halved by a max pool layer and then halved again by the second max pool layer
        dense_layers.append(DenseLayer(1024, 10, "logits_layer", lambda x:x))  #logits layer no nonlinearity

        self.conv_layers = conv_layers
        self.dense_layers = dense_layers

        self.fooling_input = tf.get_variable(name="fooling_number", shape=(1, 28, 28, 1), initializer=tf.glorot_normal_initializer())
        self.fooling_logits, self.fooling_predicted_classes = self.forward_classifier(self.fooling_input)
        self.fooling_label = tf.placeholder(dtype=tf.float32, shape=(1, 10))
        self.fooling_loss = tf.losses.softmax_cross_entropy(onehot_labels=self.fooling_label, logits=self.fooling_logits)
        self.fooling_train_op = tf.train.AdamOptimizer().minimize(self.fooling_loss, var_list=[self.fooling_input])
Пример #3
0
def create_player_atari(thread_idx, is_theta=True):
    """
    Creates a player, its NeuralNet and all the layers
    Returns the player
    """
    player = ActorA3C(game_name=constants.ROM,
                      rand_seed=constants.INITIAL_SEED,
                      gamma=constants.GAMMA,
                      thread_index=thread_idx)

    lay_conv1 = ConvLayer(input_channel=constants.SKIPED_FRAMES,
                          output_channel=constants.CONV1_FILTERS,
                          kernel_size=constants.CONV1_SIZE,
                          stride=constants.CONV1_STRIDE,
                          is_weights_init=is_theta)

    lay_conv2 = ConvLayer(input_channel=constants.CONV1_FILTERS,
                          output_channel=constants.CONV2_FILTERS,
                          kernel_size=constants.CONV2_SIZE,
                          stride=constants.CONV2_STRIDE,
                          is_weights_init=is_theta)

    lay_fc3 = FCLayer(constants.FC_LSTM_UNITS,
                      constants.FC_LSTM_OUTPUTS,
                      is_weights_init=is_theta)

    lay_fc4 = FCLayer(constants.FC_PI_UNITS,
                      len(player.game_state.real_actions),
                      is_weights_init=is_theta)

    lay_fc5 = FCLayer(constants.FC_V_UNITS,
                      constants.FC_V_OUTPUTS,
                      is_weights_init=is_theta)

    player.local_network.add_layer(lay_conv1, constants.CONV1_POS)
    player.local_network.add_layer(ReLULayer(), constants.RELU1_POS)
    player.local_network.add_layer(lay_conv2, constants.CONV2_POS)
    player.local_network.add_layer(ReLULayer(), constants.RELU2_POS)
    player.local_network.add_layer(FlattenLayer(), constants.FLATTEN_POS)
    player.local_network.add_layer(lay_fc3, constants.FC_LSTM_POS)
    player.local_network.add_layer(ReLULayer(), constants.RELU3_POS)
    player.local_network.add_layer(lay_fc4, constants.FC_PI_POS)
    player.local_network.add_layer(SoftmaxLayer(), constants.SM_POS)
    player.local_network.add_layer(lay_fc5, constants.FC_V_POS)

    return player
Пример #4
0
def execute_srcnn(n_epochs=1000,
                  image_height=232,
                  image_width=232,
                  resume=False):
    """
    :param n_epochs:
    :param batch_size: minibatch_size to train
    :param image_height:
    :param image_width:
    :return:
    """

    # Pre setup
    if not os.path.exists(training_model_folder):
        print('os.getcwd() ', os.getcwd())
        print(training_model_folder + ' does not exist!')
        return
    print('start training for', training_model_folder)
    if not os.path.exists(training_process_folder):
        os.makedirs(training_process_folder)

    if resume:
        train_log_file = open(
            os.path.join(training_model_folder, train_log_file_name), 'a')
        print('resuming from here!')
        print('\n resuming from here!', file=train_log_file)
    else:
        train_log_file = open(
            os.path.join(training_model_folder, train_log_file_name), 'w')

    # model data loading
    f = open(os.path.join(training_model_folder, 'train.json'))
    model_data = json.load(f)
    rng = np.random.RandomState(model_data["rng_seed"])

    # #of feature map = 1 - Y only from YCbCr, you need to modify a bit to support 3 - RGB channel
    input_channel_number = model_data["input_channel"]
    layer_count = model_data["layer_count"]
    batch_size = model_data["minibatch_size"]
    layers = model_data["layers"]

    image_padding = 0
    for i in np.arange(layer_count):
        # currently it only supports filter height == filter width
        assert layers[i]["filter_height"] == layers[i][
            "filter_width"], "filter height and filter width must be same!"
        assert layers[i][
            "filter_height"] % 2 == 1, "Only odd number filter is supported!"
        image_padding += layers[i]["filter_height"] - 1

    total_image_padding = image_padding

    # training data loading
    datasets = load_data()

    np_train_dataset, np_valid_dataset, np_test_dataset = datasets
    np_train_set_x, np_train_set_y = np_train_dataset
    np_valid_set_x, np_valid_set_y = np_valid_dataset
    np_test_set_x, np_test_set_y = np_test_dataset

    # print('train_set_x.shape[0]', train_set_x.shape[0].eval())
    # PREPROCESSING
    start_time = timeit.default_timer()
    train_scaled_x = preprocess(np_train_set_x, total_image_padding // 2)
    valid_scaled_x = preprocess(np_valid_set_x, total_image_padding // 2)
    test_scaled_x = preprocess(np_test_set_x, total_image_padding // 2)
    end_time = timeit.default_timer()
    print('preprocess time %i sec' % (end_time - start_time))
    print('preprocess time %i sec' % (end_time - start_time),
          file=train_log_file)

    # print('scaled_x', scaled_x)

    def shared_dataset(data, borrow=True):
        shared_data = theano.shared(np.asarray(data,
                                               dtype=theano.config.floatX),
                                    borrow=borrow)
        return shared_data

    train_set_x = shared_dataset(train_scaled_x)
    valid_set_x = shared_dataset(valid_scaled_x)
    test_set_x = shared_dataset(test_scaled_x)
    train_set_y = shared_dataset(np_train_set_y / 256.)  # normalize
    valid_set_y = shared_dataset(np_valid_set_y / 256.)
    test_set_y = shared_dataset(np_test_set_y / 256.)

    train_set_batch_size = np_train_set_x.shape[0]
    n_train_batches = np_train_set_x.shape[0] // batch_size
    n_valid_batches = np_valid_set_x.shape[0] // batch_size
    n_test_batches = np_test_set_x.shape[0] // batch_size

    # SHOW Test images (0~5)
    test_img_batch = test_set_x.get_value(borrow=False)[0:5]  # OK.
    for i in np.arange(5):
        cv2.imwrite(
            os.path.join(training_process_folder,
                         'photo' + str(i) + '_input.jpg'),
            test_img_batch[i].transpose(1, 2, 0) * 256.)

    # allocate symbolic variables for the data
    index = T.lscalar()  # index to a minibatch
    indexes = T.lvector()  # index randomizer
    x = T.tensor4(
        name='x'
    )  # input data (rasterized images): (batch_size, ch, image_height, image_width)
    y = T.tensor4(
        name='y'
    )  # output data (rasterized images): (batch_size, ch, image_height, image_width)

    # BUILD MODEL
    print('... building the model')
    if resume:
        param_lists = pickle.load(
            open(os.path.join(training_model_folder, 'best_model.pkl')))
    layer0_input = x.reshape(
        (batch_size, input_channel_number, image_height + total_image_padding,
         image_width + total_image_padding
         ))  # shape(batch_size, #of feature map, image height, image width)

    ConvLayers = []
    updates = []

    for i in np.arange(layer_count):
        if i == 0:
            previous_layer_channel = input_channel_number
            layer_input = layer0_input
        else:
            previous_layer_channel = layers[i - 1]["channel"]
            layer_input = ConvLayers[-1].output

        #print('[DEBUG], i = %i, layers[i]["channel"] = %i, layers[i]["filter_height"] = %i, layers[i]["filter_width"] = %i' %
        #      (i, layers[i]["channel"], layers[i]["filter_height"], layers[i]["filter_width"]))
        if resume:
            layer = ConvLayer(rng,
                              input=layer_input,
                              image_shape=(batch_size, previous_layer_channel,
                                           image_height + image_padding,
                                           image_width + image_padding),
                              filter_shape=(layers[i]["channel"],
                                            previous_layer_channel,
                                            layers[i]["filter_height"],
                                            layers[i]["filter_width"]),
                              use_adam=True,
                              W_values=param_lists[i][0],
                              b_values=param_lists[i][1])
        else:
            layer = ConvLayer(rng,
                              input=layer_input,
                              image_shape=(batch_size, previous_layer_channel,
                                           image_height + image_padding,
                                           image_width + image_padding),
                              filter_shape=(layers[i]["channel"],
                                            previous_layer_channel,
                                            layers[i]["filter_height"],
                                            layers[i]["filter_width"]),
                              use_adam=True)
        ConvLayers.append(layer)
        image_padding -= (layers[i]["filter_height"] - 1)
        # print('[DEBUG] image_padding = ', image_padding)

    # crete train model
    # alpha = 0.001
    beta1 = 0.9
    beta2 = 0.999
    epsilon = 0.1  # 0.000001
    beta1_t = theano.shared(
        value=np.cast['float32'](0.0),
        borrow=True)  # Casting float32 is necessary for GPU usage
    beta2_t = theano.shared(value=np.cast['float32'](.9), borrow=True)

    cost = ConvLayers[-1].cost(y)

    updates.append((beta1_t, beta1_t * beta1))
    updates.append((beta2_t, beta1_t * beta2))
    for i in np.arange(layer_count):
        params = ConvLayers[i].params
        params_m = ConvLayers[i].params_m  # for adam
        params_v = ConvLayers[i].params_v  # for adam
        gparams = T.grad(cost, params)
        for param, gparam, param_m, param_v in zip(params, gparams, params_m,
                                                   params_v):
            # Adam
            updates.append((param_m, beta1 * param_m + (1 - beta1) * gparam))
            updates.append(
                (param_v, beta2 * param_v + (1 - beta2) * gparam * gparam))
            updates.append(
                (param, param - layers[i]["learning_rate"] * param_m /
                 (1. - beta1_t) / (T.sqrt(param_v / (1 - beta2_t)) + epsilon)))
            # Normal SGD
            # updates.append((param, param - layers[i]["learning_rate"] * gparam))

    # indexes is used for image sequence randomizer for training
    train_model = theano.function(
        [index, indexes],
        cost,
        updates=updates,
        givens={
            x:
            train_set_x[indexes[index * batch_size:(index + 1) * batch_size]],
            y:
            train_set_y[indexes[index * batch_size:(index + 1) * batch_size]]
        })
    # create a test function
    # TODO: implement error function (test evaluation function, e.g. PSNR), instead of using cost function
    test_model = theano.function(
        [index],
        ConvLayers[-1].cost(y),
        givens={
            x: test_set_x[index * batch_size:(index + 1) * batch_size],
            y: test_set_y[index * batch_size:(index + 1) * batch_size]
        })

    validate_model = theano.function(
        [index],
        ConvLayers[-1].cost(y),
        givens={
            x: valid_set_x[index * batch_size:(index + 1) * batch_size],
            y: valid_set_y[index * batch_size:(index + 1) * batch_size]
        })

    construct_photo = theano.function(
        [index],
        ConvLayers[-1].output,
        givens={x: test_set_x[index * batch_size:(index + 1) * batch_size]})

    # TRAIN MODEL
    print('... training')
    patience = 30000  # 10000
    patience_increase = 2
    improvement_threshold = 0.998  # 0.995

    validation_frequency = min(n_train_batches, patience // 2) * 2

    best_validation_loss = np.inf
    best_iter = 0
    test_score = 0.
    start_time = timeit.default_timer()

    epoch = 0
    done_looping = False

    while (epoch < n_epochs) and (not done_looping):
        start_time_for_each_epoch = timeit.default_timer()
        epoch += 1
        mean_cost = []
        random_indexes = np.random.permutation(
            train_set_batch_size)  # index randomizer
        for minibatch_index in range(n_train_batches):
            iter = (epoch - 1) * n_train_batches + minibatch_index

            if iter < 10:
                img_batch = construct_photo(0)
                img0 = img_batch[0].transpose(1, 2, 0) * 256.
                print('[DEBUG] iter ', iter, ' img0: ', img0)

            if iter % 100 == 0:
                print('training @ iter ', iter)
            mean_cost += [train_model(minibatch_index, random_indexes)]

            if (iter + 1) % validation_frequency == 0:
                validation_losses = [
                    validate_model(i) for i in range(n_valid_batches)
                ]
                this_validation_loss = np.mean(validation_losses)
                print('epoch %i, minibatch %i/%i, validation cost %f' %
                      (epoch, minibatch_index + 1, n_train_batches,
                       this_validation_loss))
                print('epoch %i, minibatch %i/%i, validation cost %f' %
                      (epoch, minibatch_index + 1, n_train_batches,
                       this_validation_loss),
                      file=train_log_file)

                if this_validation_loss < best_validation_loss:
                    if this_validation_loss < best_validation_loss * improvement_threshold:
                        patience = max(patience, iter * patience_increase)
                        print('update patience -> ', patience, ' iter')
                        # we will execute at least patience iter.

                    best_validation_loss = this_validation_loss
                    best_iter = iter

                    test_losses = [
                        test_model(i) for i in range(n_test_batches)
                    ]
                    test_score = np.mean(test_losses)
                    print(
                        '     epoch %i, minibatch %i/%i, test cost of best model %f'
                        % (epoch, minibatch_index + 1, n_train_batches,
                           test_score))
                    print(
                        '     epoch %i, minibatch %i/%i, test cost of best model %f'
                        % (epoch, minibatch_index + 1, n_train_batches,
                           test_score),
                        file=train_log_file)
                    # Save best model
                    with open(
                            os.path.join(training_model_folder,
                                         'best_model.pkl'), 'wb') as f:
                        param_lists = []
                        for i in np.arange(layer_count):
                            param_lists.append([
                                ConvLayers[i].W.get_value(),
                                ConvLayers[i].b.get_value()
                            ])
                        pickle.dump(param_lists, f)

            if patience <= iter:
                done_looping = True
                break
        end_time_for_each_epoch = timeit.default_timer()
        diff_time = end_time_for_each_epoch - start_time_for_each_epoch
        print('Training epoch %d cost is %f, took %i min %i sec' %
              (epoch, np.mean(mean_cost), diff_time / 60., diff_time % 60))
        print('Training epoch %d cost is %f, took %i min %i sec' %
              (epoch, np.mean(mean_cost), diff_time / 60., diff_time % 60),
              file=train_log_file)

        # for checking/monitoring the training process
        if epoch // 10 == 0 or epoch % 10 == 0:
            photo_num = 0
            loop = 0
            while photo_num < 5:
                img_batch = construct_photo(loop)
                for j in np.arange(batch_size):
                    if photo_num == 0:
                        print('output_img0: ',
                              img_batch[j].transpose(1, 2, 0) * 256.)
                    cv2.imwrite(
                        os.path.join(
                            training_process_folder, 'photo' + str(photo_num) +
                            '_epoch' + str(epoch) + '.jpg'),
                        img_batch[j].transpose(1, 2, 0) * 256.)
                    photo_num += 1
                    if photo_num == 5:
                        break
                loop += 1

    end_time = timeit.default_timer()

    print('Optimization complete.')
    print('Best validation score of %f %% obtained at iteration %i, '
          'with test performance %f' %
          (best_validation_loss, best_iter + 1, test_score))
    print(
        ('The code for file ' + os.path.split(__file__)[1] + ' ran for %.2fm' %
         ((end_time - start_time) / 60.)),
        file=sys.stderr)

    print('Optimization complete.', file=train_log_file)
    print('Best validation score of %f obtained at iteration %i, '
          'with test performance %f' %
          (best_validation_loss, best_iter + 1, test_score),
          file=train_log_file)
    print(
        ('The code for file ' + os.path.split(__file__)[1] + ' ran for %.2fm' %
         ((end_time - start_time) / 60.)),
        file=train_log_file)
Пример #5
0
def predict_test_set(image_height=232,
                     image_width=232,
                     model_folder=training_model_folder):
    print('predict...')
    if not os.path.exists(model_folder):
        print('os.getcwd() ', os.getcwd())
        print(model_folder + ' does not exist!')
        return

    training_process_folder = os.path.join(model_folder, 'training_process')
    f = open(os.path.join(model_folder, 'train.json'), 'r')
    model_data = json.load(f)

    rng = np.random.RandomState(model_data["rng_seed"])
    input_channel_number = model_data["input_channel"]
    layer_count = model_data["layer_count"]
    batch_size = model_data["minibatch_size"]
    layers = model_data["layers"]

    image_padding = 0
    for i in np.arange(layer_count):
        # currently it only supports filter height == filter width
        assert layers[i]["filter_height"] == layers[i][
            "filter_width"], "filter height and filter width must be same!"
        assert layers[i][
            "filter_height"] % 2 == 1, "Only odd number filter is supported!"
        image_padding += layers[i]["filter_height"] - 1

    total_image_padding = image_padding

    index = T.lscalar()  # index to a minibatch
    x = T.tensor4(
        name='x'
    )  # input data (rasterized images): (batch_size, ch, image_height, image_width)
    y = T.tensor4(
        name='y'
    )  # output data (rasterized images): (batch_size, ch, image_height, image_width)
    layer0_input = x  # x.reshape((batch_size, input_channel_number, image_height, image_width))  # shape(batch_size, #of feature map, image height, image width)

    param_lists = pickle.load(
        open(os.path.join(model_folder, 'best_model.pkl')))
    #print('param_lists', param_lists)
    #print('param_lists1', param_lists[1])
    ConvLayers = []

    for i in np.arange(layer_count):
        if i == 0:
            previous_layer_channel = input_channel_number
            layer_input = layer0_input
        else:
            previous_layer_channel = layers[i - 1]["channel"]
            layer_input = ConvLayers[-1].output

        layer = ConvLayer(rng,
                          input=layer_input,
                          image_shape=(batch_size, previous_layer_channel,
                                       image_height + image_padding,
                                       image_width + image_padding),
                          filter_shape=(layers[i]["channel"],
                                        previous_layer_channel,
                                        layers[i]["filter_height"],
                                        layers[i]["filter_width"]),
                          W_values=param_lists[i][0],
                          b_values=param_lists[i][1])
        #print('layer.W', layer.W, layer.W.get_value())
        #print('layer.b', layer.b, layer.b.get_value())
        ConvLayers.append(layer)

    #print('ConvLayers', ConvLayers)
    # crete train model
    cost = ConvLayers[-1].cost(y)

    datasets = load_data()
    train_dataset, valid_dataset, test_dataset = datasets
    # train_set_x, train_set_y = train_dataset
    # valid_set_x, valid_set_y = valid_dataset
    np_test_set_x, np_test_set_y = test_dataset

    # PREPROCESSING
    test_scaled_x = preprocess(np_test_set_x, total_image_padding // 2)
    test_set_x = theano.shared(np.asarray(test_scaled_x,
                                          dtype=theano.config.floatX),
                               borrow=True)
    #print('test_scaled_x', test_scaled_x)

    construct_photo_predict = theano.function(
        [index],
        ConvLayers[-1].output,
        givens={x: test_set_x[index * batch_size:(index + 1) * batch_size]})

    photo_num = 0
    loop = 0
    while photo_num < 5:
        img_batch = construct_photo_predict(loop)
        for j in np.arange(batch_size):
            if photo_num == 0:
                print('output_img0: ', img_batch[j].transpose(1, 2, 0) * 256.)
            cv2.imwrite(
                os.path.join(training_process_folder,
                             'photo' + str(photo_num) + '_predict.jpg'),
                img_batch[j].transpose(1, 2, 0) * 256.)
            photo_num += 1
            if photo_num == 5:
                break
        loop += 1
Пример #6
0
img_w = 8  # 入力画像の幅
img_ch = 1  # 入力画像のチャンネル数

wb_width = 0.1  # 重みとバイアスの広がり具合
eta = 0.01  # 学習係数
epoch = 1
batch_size = 8
interval = 1  # 経過の表示間隔
n_sample = 7  # 誤差計測のサンプル数
n_flt = 6  # n_flt:フィルタ数
flt_h = 3  # flt_h:フィルタ高さ
flt_w = 3  # flt_w:フィルタ幅
# stride:ストライド幅, pad:パディング幅

# -- 各層の初期化 --
cl_1 = ConvLayer(img_ch, img_h, img_w, n_flt, flt_h, flt_w, 1, 1)
pl_1 = PoolingLayer(cl_1.y_ch, cl_1.y_h, cl_1.y_w, 2, 0)

n_fc_in = pl_1.y_ch * pl_1.y_h * pl_1.y_w
ml_1 = MiddleLayer(n_fc_in, 10)
ol_1 = OutputLayer(10, 10)


# -- 順伝播 --
def forward_propagation(x):
    n_bt = x.shape[0]

    print("forward_propagation()")
    print("  x.shape", x.shape)
    images = x.reshape(n_bt, img_ch, img_h, img_w)
    print("  x.reshape", images.shape)
Пример #7
0
    def num_ops(self):
        return np.sum([layer.num_ops for layer in self.layers])

    def __str__(self):
        return '\n'.join([
            '%s %10.6f %10.6f' %
            (layer, layer.num_ops * 1e-9, layer.num_params * 1e-6)
            for layer in self.layers
        ])

    def __repr__(self):
        return self.__str__()


VGG16 = ConvNet([
    ConvLayer("conv1_1", 224, 224, 3, 64, 3, STD),
    ConvLayer("conv1_2", 224, 224, 64, 64, 3, STD),
    ConvLayer("conv2_1", 112, 112, 64, 128, 3, STD),
    ConvLayer("conv2_2", 112, 112, 128, 128, 3, STD),
    ConvLayer("conv3_1", 56, 56, 128, 256, 3, STD),
    ConvLayer("conv3_2", 56, 56, 256, 256, 3, STD),
    ConvLayer("conv3_3", 56, 56, 256, 256, 3, STD),
    ConvLayer("conv4_1", 28, 28, 256, 512, 3, STD),
    ConvLayer("conv4_2", 28, 28, 512, 512, 3, STD),
    ConvLayer("conv4_3", 28, 28, 512, 512, 3, STD),
    ConvLayer("conv5_1", 14, 14, 512, 512, 3, STD),
    ConvLayer("conv5_2", 14, 14, 512, 512, 3, STD),
    ConvLayer("conv5_3", 14, 14, 512, 512, 3, STD),
    MatmulLayer("fc6", 7 * 7 * 512, 4096),
    MatmulLayer("fc7", 4096, 4096),
    MatmulLayer("fc8", 4096, 1000),
Пример #8
0
def srcnn2x(photo_file_path,
            output_file_path,
            model_folder=model_folder,
            compare=False):
    print('srcnn2x: ', photo_file_path, ' with model ', model_folder)
    if not os.path.exists(model_folder):
        print('os.getcwd() ', os.getcwd())
        print(model_folder + ' does not exist!')
        return

    input_img = cv2.imread(photo_file_path, cv2.IMREAD_COLOR)
    input_image_height = input_img.shape[0]
    input_image_width = input_img.shape[1]
    output_image_height = 2 * input_image_height
    output_image_width = 2 * input_image_width
    f = open(os.path.join(model_folder, 'train.json'), 'r')
    model_data = json.load(f)

    rng = np.random.RandomState(model_data["rng_seed"])
    input_channel_number = model_data["input_channel"]
    layer_count = model_data["layer_count"]
    #batch_size = model_data["minibatch_size"]
    batch_size = 1  # We will convert one photo file
    layers = model_data["layers"]

    image_padding = 0
    for i in np.arange(layer_count):
        # currently it only supports filter height == filter width
        assert layers[i]["filter_height"] == layers[i][
            "filter_width"], "filter height and filter width must be same!"
        assert layers[i][
            "filter_height"] % 2 == 1, "Only odd number filter is supported!"
        image_padding += layers[i]["filter_height"] - 1

    total_image_padding = image_padding

    index = T.lscalar()  # index to a minibatch
    x = T.tensor4(
        name='x'
    )  # input data (rasterized images): (batch_size, ch, image_height, image_width)
    y = T.tensor4(
        name='y'
    )  # output data (rasterized images): (batch_size, ch, image_height, image_width)
    layer0_input = x  # x.reshape((batch_size, input_channel_number, output_image_height, output_image_width))
    # shape(batch_size, #of feature map, image height, image width)

    param_lists = pickle.load(
        open(os.path.join(model_folder, 'best_model.pkl')))
    #print('param_lists1', param_lists[1])
    #print('param_lists.shape', param_lists.shape)
    conv_layers = []

    for i in np.arange(layer_count):
        if i == 0:
            previous_layer_channel = input_channel_number
            layer_input = layer0_input
        else:
            previous_layer_channel = layers[i - 1]["channel"]
            layer_input = conv_layers[-1].output

        # print('[DEBUG], i = %i, layers[i]["channel"] = %i, layers[i]["filter_height"] = %i, layers[i]["filter_width"] = %i' %
        #       (i, layers[i]["channel"], layers[i]["filter_height"], layers[i]["filter_width"]))
        layer = ConvLayer(rng,
                          input=layer_input,
                          image_shape=(batch_size, previous_layer_channel,
                                       output_image_height + image_padding,
                                       output_image_width + image_padding),
                          filter_shape=(layers[i]["channel"],
                                        previous_layer_channel,
                                        layers[i]["filter_height"],
                                        layers[i]["filter_width"]),
                          W_values=param_lists[i][0],
                          b_values=param_lists[i][1])
        conv_layers.append(layer)
    # crete train model
    cost = conv_layers[-1].cost(y)

    ycc_input_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2YCR_CB)

    data_prescaled_x = np.empty(
        (1, input_channel_number, input_image_height, input_image_width))
    data_prescaled_x[0, :, :, :] = np.transpose(
        ycc_input_img[:, :, 0:1], (2, 0, 1))  # # (ch, height, width)

    # data_scaled_x = np.empty((1, input_channel_number, output_image_height, output_image_width))
    # PREPROCESSING
    data_scaled_x = preprocess(data_prescaled_x, total_image_padding // 2)
    input_x = theano.shared(np.asarray(data_scaled_x,
                                       dtype=theano.config.floatX),
                            borrow=True)

    srcnn_photo_predict = theano.function([],
                                          conv_layers[-1].output,
                                          givens={x: input_x[:]})

    output_img_y = srcnn_photo_predict()  # (ch, width, height)
    #print('output_img.shape', output_img_y.shape)
    #print('output_img: ', output_img_y)

    img0 = output_img_y[0].transpose(1, 2, 0) * 256.  # (width, height, ch)
    #print('img0.shape', img0.shape)
    #print('img0: ', img0)

    scaled_input_img = cv2.resize(input_img,
                                  (output_image_width, output_image_height))
    if compare:
        cv2.imwrite(conventional_file_path, scaled_input_img)

    ycc_scaled_input_img = cv2.cvtColor(scaled_input_img, cv2.COLOR_BGR2YCR_CB)
    ycc_scaled_input_img[:, :, 0:1] = img0  # (width, height, ch)
    rgb_scaled_img = cv2.cvtColor(ycc_scaled_input_img, cv2.COLOR_YCR_CB2BGR)
    cv2.imwrite(output_file_path, rgb_scaled_img)