为CNN编写训练模型

我正在为TwoStream-IQA编写训练代码,它是一个双流卷积神经网络。该模型通过网络的两个流预测正在评估的补丁的质量分数。在下面的培训中,我使用了上面 GitHub 链接中提供的测试数据集。


训练代码如下:


import os

import time

import numpy as np

import argparse


import chainer


chainer.global_config.train=True


from chainer import cuda

from chainer import serializers

from chainer import optimizers

from chainer import iterators

from chainer import training 

from chainer.training import extensions

from PIL import Image

from sklearn.feature_extraction.image import extract_patches


from model import Model


parser = argparse.ArgumentParser(description='train.py')

parser.add_argument('--model', '-m', default='', 

                    help='path to the trained model')

parser.add_argument('--gpu', '-g', default=0, type=int, help='GPU ID')


args = parser.parse_args()



model = Model()


cuda.cudnn_enabled = True

cuda.check_cuda_available()

xp = cuda.cupy

model.to_gpu()


## prepare training data 

test_label_path = 'data_list/test.txt'

test_img_path = 'data/live/'

test_Graimg_path = 'data/live_grad/'

save_model_path = '/models/nr_sana_2stream.model'


patches_per_img = 256

patchSize = 32


print('-------------Load data-------------')

final_train_set = []

with open(test_label_path, 'rt') as f:

    for l in f:

        line, la = l.strip().split()  # for debug


        tic = time.time()

        full_path = os.path.join(test_img_path, line)

        Grafull_path = os.path.join(test_Graimg_path, line)


        inputImage = Image.open(full_path)

        Graf = Image.open(Grafull_path)

        img = np.asarray(inputImage, dtype=np.float32)

        Gra = np.asarray(Graf, dtype=np.float32)

        img = img.transpose(2, 0, 1)

        Gra = Gra.transpose(2, 0, 1)


        img1 = np.zeros((1, 3, Gra.shape[1], Gra.shape[2]))

        img1[0, :, :, :] = img

        Gra1 = np.zeros((1, 3, Gra.shape[1], Gra.shape[2]))

        Gra1[0, :, :, :] = Gra

HUWWW
浏览 154回答 2
2回答

MM们

简而言之,您不恰当地调用numpy.asarray:numpy.asarray不连接两个cupy.ndarrays,而连接两个numpy.ndarrays。您的代码简介:import numpy, cupyfinal_train_set = []N_PATCH_PER_IMAGE = 8for i in range(10):&nbsp; &nbsp; label = 0&nbsp; &nbsp; temp_slice_1 = [numpy.zeros((3, 3)) for j in range(N_PATCH_PER_IMAGE)]&nbsp; &nbsp; temp_slice_2 = [numpy.zeros((3, 3)) for j in range(N_PATCH_PER_IMAGE)]&nbsp; &nbsp; for j in range(N_PATCH_PER_IMAGE):&nbsp; &nbsp; &nbsp; &nbsp; temp_slice_1[j] = cupy.array(temp_slice_1[j])&nbsp; &nbsp; &nbsp; &nbsp; temp_slice_2[j] = cupy.array(temp_slice_2[j])&nbsp; &nbsp; &nbsp; &nbsp; final_train_set.append(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # attempting to concatenate two cupy arrays by numpy.asarray&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numpy.asarray([temp_slice_1[j], temp_slice_2[j]]),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; )错误import numpy as npimport cupy as cpprint("two numpy arrays")print(np.asarray([np.zeros(shape=(1,)), np.zeros(shape=(1,))]))print(np.asarray([np.zeros(shape=(1,)), np.zeros(shape=(1,))]).dtype)print()print("two cupy arrays")print(np.asarray([cp.zeros(shape=(1,)), cp.zeros(shape=(1,))]))print(np.asarray([cp.zeros(shape=(1,)), cp.zeros(shape=(1,))]).dtype)two numpy arrays[[0.]&nbsp;[0.]]float64two cupy arrays[[array(0.)]&nbsp;[array(0.)]]object解决方法:注释掉两行import numpy&nbsp; # not import cupy herefor i in range(10):&nbsp; &nbsp; label = 0&nbsp; &nbsp; temp_slice_1 = [numpy.zeros((3, 3)) for j in range(N_PATCH_PER_IMAGE)]&nbsp; &nbsp; temp_slice_2 = [numpy.zeros((3, 3)) for j in range(N_PATCH_PER_IMAGE)]&nbsp; &nbsp; for j in range(N_PATCH_PER_IMAGE):&nbsp; &nbsp; &nbsp; &nbsp; # temp_slice_1[j] = cupy.array(temp_slice_1[j]) <- comment out!&nbsp; &nbsp; &nbsp; &nbsp; # temp_slice_2[j] = cupy.array(temp_slice_2[j]) <- comment out!&nbsp; &nbsp; &nbsp; &nbsp; final_train_set.append(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # concatenate two numpy arrays: usually cupy should not be used in dataset&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numpy.asarray([temp_slice_1[j], temp_slice_2[j]]),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; )脚注在您提供的代码中,xp未指定,因此您无法从任何人那里得到答案。如果您无法分离问题,请发布您的代码的整体,包括模型。我猜您可能由于其他原因无法运行训练代码。在这段代码中,数据首先在final_train_set.&nbsp;但是如果图像数量很大,主内存就会耗尽并被MemoryError抬高。(换句话说,如果图像数量很少,并且您的内存足够大,则不会发生错误)在这种情况下,以下参考资料(Chainer at glass和Dataset Abstraction)会有所帮助。

千巷猫影

我使用 OpenCV、Scipy 和其他一些用于质量评估的模块找到了这个Github 存储库。这是代码:# Python code for BRISQUE model# Original paper title: No-Reference Image Quality Assessment in the Spatial Domain# Link: http://ieeexplore.ieee.org/document/6272356/import cv2import numpy as npfrom scipy import ndimageimport mathdef get_gaussian_filter():&nbsp; &nbsp; [m,n] = [(ss - 1.0) / 2.0 for ss in (shape,shape)]&nbsp; &nbsp; [y,x] = np.ogrid[-m:m+1,-n:n+1]&nbsp; &nbsp; window = np.exp( -(x*x + y*y) / (2.0*sigma*sigma) )&nbsp; &nbsp; window[window < np.finfo(window.dtype).eps*window.max() ] = 0&nbsp; &nbsp; sum_window = window.sum()&nbsp; &nbsp; if sum_window != 0:&nbsp; &nbsp; &nbsp; &nbsp; window = np.divide(window, sum_window)&nbsp; &nbsp; return windowdef lmom(X):&nbsp; &nbsp; (rows, cols)&nbsp; = X.shape&nbsp; &nbsp; if cols == 1:&nbsp; &nbsp; &nbsp; &nbsp; X = X.reshape(1,rows)&nbsp; &nbsp; n = rows&nbsp; &nbsp; X.sort()&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; b = np.zeros(3)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; b0 = X.mean()&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for r in range(1,4):&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Num = np.prod(np.tile(np.arange(r+1,n+1), (r,1))-np.tile(np.arange(1,r+1).reshape(r,1),(1,n-r)),0)&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Num = Num.astype(np.float)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Den = np.prod(np.tile(n, (1, r)) - np.arange(1,r+1), 1)&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; b[r-1] = 1.0/n * sum(Num/Den * X[0,r:])&nbsp; &nbsp; L = np.zeros(4)&nbsp; &nbsp; L[0] = b0&nbsp; &nbsp; L[1] = 2*b[0] - b0&nbsp; &nbsp; L[2] = 6*b[1] - 6*b[0] + b0&nbsp; &nbsp; L[3] = 20*b[2] - 30*b[1] + 12*b[0] - b0&nbsp; &nbsp; return Ldef compute_features(im):&nbsp; &nbsp; im = im.astype(np.float)&nbsp; &nbsp; window = get_gaussian_filter()&nbsp; &nbsp; scalenum = 2&nbsp; &nbsp; feat = []&nbsp; &nbsp; for itr_scale in range(scalenum):&nbsp; &nbsp; &nbsp; &nbsp; mu = cv2.filter2D(im, cv2.CV_64F, window, borderType=cv2.BORDER_CONSTANT)&nbsp; &nbsp; &nbsp; &nbsp; mu_sq = mu * mu&nbsp; &nbsp; &nbsp; &nbsp; sigma = np.sqrt(abs(cv2.filter2D(im*im, cv2.CV_64F, window, borderType=cv2.BORDER_CONSTANT) - mu_sq))&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; structdis = (im-mu)/(sigma+1)&nbsp; &nbsp; &nbsp; &nbsp; structdis_col_vector = np.reshape(structdis.transpose(), (structdis.size,1))&nbsp; &nbsp; &nbsp; &nbsp; L = lmom(structdis.reshape(structdis.size,1))&nbsp; &nbsp; &nbsp; &nbsp; feat = np.append(feat,[L[1], L[3]])&nbsp; &nbsp; &nbsp; &nbsp; shifts = [[0,1], [1,0], [1,1], [-1,1]]&nbsp; &nbsp; &nbsp; &nbsp; for itr_shift in shifts:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shifted_structdis = np.roll(structdis, itr_shift[0], axis=0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shifted_structdis = np.roll(shifted_structdis, itr_shift[1], axis=1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shifted_structdis_col_vector = np.reshape(shifted_structdis.T, (shifted_structdis.size,1))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pair = structdis_col_vector * shifted_structdis_col_vector&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; L = lmom(pair.reshape(pair.size,1))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; feat = np.append(feat, L)&nbsp; &nbsp; &nbsp; &nbsp; im = cv2.resize(im, (0,0), fx=0.5, fy=0.5, interpolation=cv2.INTER_CUBIC)&nbsp; &nbsp; return featim = ndimage.imread('example.bmp', flatten=True)feat = compute_features(im)print feat
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python