Esempio n. 1
0
    def __init__(self, input_img, conf=Config(), ground_truth=None, kernels=None):
        # Acquire meta parameters configuration from configuration class as a class variable
        self.conf = conf
        self.cuda = conf.cuda
        # Read input image (can be either a numpy array or a path to an image file)
        self.input = input_img if type(input_img) is not str else img.imread(input_img)
        self.Y = False
        if len(self.input)==2:
            self.Y = True
        #input is ndarray
        # For evaluation purposes, ground-truth image can be supplied.
        self.gt = ground_truth if type(ground_truth) is not str else img.imread(ground_truth)
        #gt is ndarray
        # Preprocess the kernels. (see function to see what in includes).
        self.kernels = preprocess_kernels(kernels, conf)
        #downsample kernel custom
        # Prepare TF default computational graph
        # declare model here severs as initial model
        print(self.Y)
        self.model = simpleNet(self.Y)

        # Build network computational graph
        #self.build_network(conf)

        # Initialize network weights and meta parameters
        self.init_parameters()

        # The first hr father source is the input (source goes through augmentation to become a father)
        # Later on, if we use gradual sr increments, results for intermediate scales will be added as sources.
        self.hr_fathers_sources = [self.input]

        # We keep the input file name to save the output with a similar name. If array was given rather than path
        # then we use default provided by the configs
        self.file_name = input_img if type(input_img) is str else conf.name
Esempio n. 2
0
    def run(self):
        # Run gradually on all scale factors (if only one jump then this loop only happens once)
        for self.sf_ind, (sf, self.kernel) in enumerate(
                zip(self.conf.scale_factors, self.kernels)):
            # verbose
            print('** Start training for sf=', sf, ' **')

            # Relative_sf (used when base change is enabled. this is when input is the output of some previous scale)
            # safe
            if np.isscalar(sf):
                sf = [sf, sf]
            self.sf = np.array(sf) / np.array(self.base_sf)
            self.output_shape = np.uint(
                np.ceil(np.array(self.input.shape[0:2]) * sf))
            print('input shape', self.input.shape)
            # Initialize network
            # reinit all for each scale factors, each gradual level
            self.init_parameters()
            if self.conf.init_net_for_each_sf == True:
                self.model = simpleNet(self.Y)
            if self.cuda:
                self.model = self.model.cuda()

            # Train the network
            # should be modified
            self.train()

            # Use augmented outputs and back projection to enhance result. Also save the result.
            post_processed_output = self.final_test()

            # Keep the results for the next scale factors SR to use as dataset
            self.hr_fathers_sources.append(post_processed_output)

            # In some cases, the current output becomes the new input. If indicated and if this is the right scale to
            # become the new base input. all of these conditions are checked inside the function.
            self.base_change()

            # Save the final output if indicated
            if self.conf.save_results:
                sf_str = ''.join('X%.2f' % s
                                 for s in self.conf.scale_factors[self.sf_ind])
                plt.imsave('%s/%s_zssr_%s.png' %
                           (self.conf.result_path,
                            os.path.basename(self.file_name)[:-4], sf_str),
                           post_processed_output,
                           vmin=0,
                           vmax=1)

            # verbose
            print('** Done training for sf=', sf, ' **')

        # Return the final post processed output.
        # noinspection PyUnboundLocalVariable
        return post_processed_output
Esempio n. 3
0
data_tf = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize([0.5], [0.5])])
train_dataset = datasets.MNIST(root='../data',
                               train=True,
                               transform=data_tf,
                               download=True)
test_dataset = datasets.MNIST(root="../data", train=False, transform=data_tf)

batch_size = 64
learning_rate = 1e-2
num_epoches = 20

train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_dataset,
                         batch_size=batch_size * 2,
                         shuffle=False)

in_dim, n_hidden_1, n_hidden_2, out_dim = 28 * 28, 300, 100, 10

model1 = simplenet.simpleNet(in_dim, n_hidden_1, n_hidden_2, out_dim)
model2 = simplenet.activationNet(in_dim, n_hidden_1, n_hidden_2, out_dim)
model3 = simplenet.batchNet(in_dim, n_hidden_1, n_hidden_2, out_dim)

for model in [model3]:
    print("the {} start traing...".format(model.get_name()))
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD(model.parameters(), 1e-1)  # 使用随机梯度下降,学习率 0.1
    train(model, train_loader, test_loader, 20, optimizer, criterion)
    print("the {} complete traing...".format(model.get_name()))