huangzehao / caffe-vdsr

A Caffe-based implementation of very deep convolution network for image super-resolution
MIT License
273 stars 134 forks source link

Test Function at Caffe #34

Closed melgor closed 7 years ago

melgor commented 7 years ago

Hi, I'm trying to code the test function without the MatLab (which I do not have). But I cannot get any good results, it look. I have some question about the testing code:

  1. While testing you divide the image by 255.0 (so you have image between 0-1). Why are you doing that? I did not see any such normalization when you are creating a training data? I try to add this to my code but it does not work for my case.
  2. When I'm predicting the Y channel, the values from it are from -40 to 260, so there are not uint8. Aftter conversion it to uint8, I have some white/black spot. I haven't found any code which will resolve such case in that repo. Do you handle it any way?

Here is my Python code for Caffe:

import cv2
import numpy as np
import os
import sys
import caffe

class SuperResolutionNet(object):
    '''Predict the image which is rescaled version of original '''
    def __init__(self, config):
        caffe.set_mode_cpu()
        self.network = caffe.Net(config.deploy, config.extract, caffe.TEST)

    def predictImage(self, image, resize):
        # Get Image of desire size in YCrCb color space      
        h,w,c                                = image.shape
        hs                                   = int(np.ceil(h*resize))
        ws                                   = int(np.ceil(w*resize))
        # Change color space to YCrCb and get Y channel resized to new size
        img_color                            = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)
        img_Y                                = img_color[:,:,0]
        img_Y                                = cv2.resize(img_Y, (ws,hs)).reshape(1, ws, hs)

        # Get Output of Y channel from Network
        self.network.blobs['data'].reshape(1, 1, hs, ws)
        self.network.blobs['data'].data[...] = img_Y.astype(np.float32)
        out                                  = self.network.forward()

        im_data                              = cv2.resize(img_color, (ws,hs))
        # Replace Y channel with predicted one
        im_data[:,:,0]                       = out['sum'].astype(np.uint8)

        # Get the BGR image 
        img_resized                          = cv2.cvtColor(im_data, cv2.COLOR_YCrCb2BGR)
        return img_resized

if __name__ == "__main__":
    config = lambda: None
    config.deploy  = 'train_model/VDSR_deploy.prototxt'
    config.extract = 'train_model/VDSR_Adam.caffemodel'
    config.resize  = 3.0
    spNet = SuperResolutionNet(config)
    image = cv2.imread(sys.argv[1])
    image_small = cv2.resize(image, None,fx=1.0/config.resize, fy=1.0/config.resize)
    image_resized = spNet.predictImage(image_small, config.resize)
    cv2.imwrite("resized_net.jpg", image_resized)

    image_cubic = cv2.resize(image_small, None,fx=config.resize, fy=config.resize)
    cv2.imwrite("resized_cubic.jpg", image_cubic)
huangzehao commented 7 years ago

Hi, (1) https://github.com/huangzehao/caffe-vdsr/blob/master/Train/generate_train.m#L24 this line convert the image from 0-255 to 0-1. (2) You can follow the implementation of scn, https://github.com/huangzehao/SCN_Matlab/tree/master/python_iccv.

ghost commented 7 years ago

@melgor : Are you able to fix this issue? I am having similar issues. Could you please share your code if its working. I am having output but its not good like that from matconvnet.