- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用卷积神经网络对图像进行分类。我经历过这个 tutorial关于深度学习并实现了给定的 code有很多修改。我添加了更多的卷积层和最大池化层,并更改了输入以接受 166x166 的输入。为了在训练后保存参数,我们在为每一层(ConvPool、FullyConnected 和 Softmax)分别定义的函数 save() 中使用了 cPickle.dump()。该函数在训练完成后在 sgd() 方法中针对所有层调用。在另一个程序中,softmax、全连接层和卷积层的参数是从另一个程序中的 .p pickled 文件加载回来的,除了我们没有调用 SGD 方法之外。问题是,我想打印 Softmax 层的 y_out(y_out 用于计算我们网络的精度),以获得图像类别的预测。但是在尝试之后
#print net.layers[-1].y_out.eval()
#x2 = net.layers[-1].y_out
#y2 = T.cast(x2, 'int32')
#print (pp(net.layers[-1].y_out))
#help(T.argmax)
#print net.layers[-1].y_out.shape.eval()
当我使用 eval() 函数获取 Tensor 变量的值时,我仍然得到 'argmax' 作为 Tensor 变量的值,' y_out 或其他错误 Missing Input Error。
因此需要帮助来打印单个测试图像的预测。
这是我们修改后network3.py(更名为net3.py)的代码:
"""net3.py
~~~~~~~~~~~~~~
A Theano-based program for training and running simple neural
networks.
Supports several layer types (fully connected, convolutional, max
pooling, softmax), and activation functions (sigmoid, tanh, and
rectified linear units, with more easily added).
When run on a CPU, this program is much faster than network.py and
network2.py. However, unlike network.py and network2.py it can also
be run on a GPU, which makes it faster still.
Because the code is based on Theano, the code is different in many
ways from network.py and network2.py. However, where possible I have
tried to maintain consistency with the earlier programs. In
particular, the API is similar to network2.py. Note that I have
focused on making the code simple, easily readable, and easily
modifiable. It is not optimized, and omits many desirable features.
This program incorporates ideas from the Theano documentation on
convolutional neural nets (notably,
http://deeplearning.net/tutorial/lenet.html ), from Misha Denil's
implementation of dropout (https://github.com/mdenil/dropout ), and
from Chris Olah (http://colah.github.io ).
"""
#tts refers to trying to save
#### Libraries
# Standard library
import json
import cPickle
import gzip
import load
# Third-party libraries
import numpy as np
import theano
import theano.tensor as T
from theano.tensor.nnet import conv
from theano.tensor.nnet import softmax
from theano.tensor import shared_randomstreams
from theano.tensor.signal import downsample
from theano import pp
# Activation functions for neurons
def linear(z): return z
def ReLU(z): return T.maximum(0.0, z)
from theano.tensor.nnet import sigmoid
from theano.tensor import tanh
'''
#### Constants
GPU = True
if GPU:
try: theano.config.device = 'gpu'
except: pass # it's already set
print "Trying to run under a GPU. If this is not desired, then modify "+\
"network3.py\nto set the GPU flag to False."
theano.config.floatX = 'float32'
else:
print "Running with a CPU. If this is not desired, then the modify "+\
"network3.py to set\nthe GPU flag to True."
'''
print "DEVICE IS:" ,theano.config.device
#### Load the MNIST data
def load_data_shared(filename="../data/mnist.pkl.gz"):
f = gzip.open(filename, 'rb')
training_data, validation_data, test_data = cPickle.load(f)
f.close()
def shared(data):
"""Place the data into shared variables. This allows Theano to copy
the data to the GPU, if one is available.
"""
shared_x = theano.shared(
np.asarray(data[0], dtype=theano.config.floatX), borrow=True)
shared_y = theano.shared(
np.asarray(data[1], dtype=theano.config.floatX), borrow=True)
return shared_x, T.cast(shared_y, "int32")
return [shared(training_data), shared(validation_data), shared(test_data)]
def load_mydata_shared():
test_data = load.load_data()
def shared(data):
"""Place the data into shared variables. This allows Theano to copy
the data to the GPU, if one is available.
"""
print "data:",data
shared_x = theano.shared(
np.asarray(data[0], dtype=theano.config.floatX))
shared_y = theano.shared(
np.asarray(data[1], dtype=theano.config.floatX))
return shared_x, T.cast(shared_y, "int32")
return [shared(test_data)]
#### Main class used to construct and train networks
class Network(object):
def __init__(self, layers, mini_batch_size):
"""Takes a list of `layers`, describing the network architecture, and
a value for the `mini_batch_size` to be used during training
by stochastic gradient descent.
"""
self.layers = layers
self.mini_batch_size = mini_batch_size
self.params = [param for layer in self.layers for param in layer.params]
self.x = T.matrix("x")
self.y = T.ivector("y")
# self.x1 = T.matrix('x')
# self.y1 = T.ivector('y')
# self.x2 = T.matrix('x')
# self.y2 = T.ivector('y')
init_layer = self.layers[0]
init_layer.set_inpt(self.x, self.x, self.mini_batch_size)
for j in xrange(1, len(self.layers)):
prev_layer, layer = self.layers[j-1], self.layers[j]
layer.set_inpt(
prev_layer.output, prev_layer.output_dropout, self.mini_batch_size)
self.output = self.layers[-1].output
self.output_dropout = self.layers[-1].output_dropout
print "class issss:",pp(T.cast(self.layers[-1].y_out.shape,'int32'))
def SGD(self, epochs, mini_batch_size, eta,
test_data, lmbda=0.0):
"""Train the network using mini-batch stochastic gradient descent."""
test_x, test_y = test_data
print "tex:",test_x
print "tey:",test_y
# compute number of minibatches for training, validation and testing
print "Epochs:"+str(epochs)
print "Mini-batch size:"+str(mini_batch_size)
print "Eta:"+str(eta)
num_test_batches = size(test_data)/mini_batch_size
# define the (regularized) cost function, symbolic gradients, and updates
l2_norm_squared = sum([(layer.w**2).sum() for layer in self.layers])
# define functions to train a mini-batch, and to compute the
# accuracy in validation and test mini-batches.
i = T.lscalar() # mini-batch index
test_mb_accuracy = theano.function(
[i], self.layers[-1].accuracy(self.y),
givens={
self.x:
test_x[i*self.mini_batch_size: (i+1)*self.mini_batch_size],
self.y:
test_y[i*self.mini_batch_size: (i+1)*self.mini_batch_size]
})
self.test_mb_predictions = theano.function(
[i], self.layers[-1].y_out,
givens={
self.x:
test_x[i*self.mini_batch_size: (i+1)*self.mini_batch_size]
})
test_accuracy = np.mean([test_mb_accuracy(j) for j in xrange(num_test_batches)])
print('The corresponding test accuracy is {0:.2%}'.format(test_accuracy))
print("Finished training network.")
print("Best validation accuracy of {0:.2%} obtained at iteration {1}".format(
best_validation_accuracy, best_iteration))
print("Corresponding test accuracy of {0:.2%}".format(test_accuracy))
#### Define layer types
class ConvPoolLayer(object):
"""Used to create a combination of a convolutional and a max-pooling
layer. A more sophisticated implementation would separate the
two, but for our purposes we'll always use them together, and it
simplifies the code, so it makes sense to combine them.
"""
def __init__(self, filter_shape, image_shape, poolsize=(2, 2),
activation_fn=ReLU):
"""`filter_shape` is a tuple of length 4, whose entries are the number
of filters, the number of input feature maps, the filter height, and the
filter width.
`image_shape` is a tuple of length 4, whose entries are the
mini-batch size, the number of input feature maps, the image
height, and the image width.
`poolsize` is a tuple of length 2, whose entries are the y and
x pooling sizes.
"""
self.filter_shape = filter_shape
self.image_shape = image_shape
self.poolsize = poolsize
self.activation_fn=activation_fn
# initialize weights and biases
n_out = (filter_shape[0]*np.prod(filter_shape[2:])/np.prod(poolsize))
self.w = theano.shared(
np.asarray(
np.random.normal(loc=0, scale=np.sqrt(1.0/n_out), size=filter_shape),
dtype=theano.config.floatX),
borrow=True)
self.b = theano.shared(
np.asarray(
np.random.normal(loc=0, scale=1.0, size=(filter_shape[0],)),
dtype=theano.config.floatX),
borrow=True)
self.load()
self.params = [self.w, self.b]
def set_inpt(self, inpt, inpt_dropout, mini_batch_size):
self.inpt = inpt.reshape(self.image_shape)
conv_out = conv.conv2d(
input=self.inpt, filters=self.w, filter_shape=self.filter_shape,
image_shape=self.image_shape)
pooled_out = downsample.max_pool_2d(
input=conv_out, ds=self.poolsize, ignore_border=True)
self.output = self.activation_fn(
pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))
self.output_dropout = self.output # no dropout in the convolutional layers
def load(self):
"""Save the neural network to the file ``filename``."""
save_file=open('/home/sweta/BE_PROJECT/tryingtosave/cl.p')
self.w.set_value(cPickle.load(save_file),borrow=True)
self.b.set_value(cPickle.load(save_file),borrow=True)
class FullyConnectedLayer(object):
def __init__(self, n_in, n_out, activation_fn=ReLU, p_dropout=0.5):
self.n_in = n_in
self.n_out = n_out
self.activation_fn = activation_fn
self.p_dropout = p_dropout
# Initialize weights and biases
self.w = theano.shared(
np.asarray(
np.random.normal(
loc=0.0, scale=np.sqrt(1.0/n_out), size=(n_in, n_out)),
dtype=theano.config.floatX),
name='w', borrow=True)
self.b = theano.shared(
np.asarray(np.random.normal(loc=0.0, scale=1.0, size=(n_out,)),
dtype=theano.config.floatX),
name='b', borrow=True)
self.load()
self.params = [self.w, self.b]
def set_inpt(self, inpt, inpt_dropout, mini_batch_size):
self.inpt = inpt.reshape((mini_batch_size, self.n_in))
self.output = self.activation_fn(
(1-self.p_dropout)*T.dot(self.inpt, self.w) + self.b)
self.y_out = T.argmax(self.output, axis=1)
self.inpt_dropout = dropout_layer(
inpt_dropout.reshape((mini_batch_size, self.n_in)), self.p_dropout)
self.output_dropout = self.activation_fn(
T.dot(self.inpt_dropout, self.w) + self.b)
def accuracy(self, y):
"Return the accuracy for the mini-batch."
return T.mean(T.eq(y, self.y_out))
def load(self):
"""Save the neural network to the file ``filename``."""
save_file=open('/home/sweta/BE_PROJECT/tryingtosave/fcl.p')
self.w.set_value(cPickle.load(save_file),borrow=True)
self.b.set_value(cPickle.load(save_file),borrow=True)
class SoftmaxLayer(object):
def __init__(self, n_in, n_out, p_dropout=0.5):
self.n_in = n_in
self.n_out = n_out
self.p_dropout = p_dropout
# Initialize weights and biases
self.w = theano.shared(
np.zeros((n_in, n_out), dtype=theano.config.floatX),
name='w', borrow=True)
self.b = theano.shared(
np.zeros((n_out,), dtype=theano.config.floatX),
name='b', borrow=True)
self.load()
self.params = [self.w, self.b]
def set_inpt(self, inpt, inpt_dropout, mini_batch_size):
self.inpt = inpt.reshape((mini_batch_size, self.n_in))
self.output = softmax((1-self.p_dropout)*T.dot(self.inpt, self.w) + self.b)
self.y_out = T.argmax(self.output, axis=1)
self.inpt_dropout = dropout_layer(
inpt_dropout.reshape((mini_batch_size, self.n_in)), self.p_dropout)
self.output_dropout = softmax(T.dot(self.inpt_dropout, self.w) + self.b)
def cost(self, net):
"Return the log-likelihood cost."
return -T.mean(T.log(self.output_dropout)[T.arange(net.y.shape[0]), net.y])
def accuracy(self, y):
"Return the accuracy for the mini-batch."
print "class is:", self.y_out
return T.mean(T.eq(y, self.y_out))
def load(self):
"""Save the neural network to the file ``filename``."""
save_file=open('/home/sweta/BE_PROJECT/tryingtosave/sml.p')
self.w.set_value(cPickle.load(save_file),borrow=True)
self.b.set_value(cPickle.load(save_file),borrow=True)
#### Miscellanea
def size(data):
"Return the size of the dataset `data`."
return data[0].get_value(borrow=True).shape[0]
def dropout_layer(layer, p_dropout):
srng = shared_randomstreams.RandomStreams(
np.random.RandomState(0).randint(999999))
mask = srng.binomial(n=1, p=1-p_dropout, size=layer.shape)
return layer*T.cast(mask, theano.config.floatX)
主要代码为:
import net3
import load
import theano.tensor as T
from theano import pp
from net3 import Network
from net3 import ConvPoolLayer, FullyConnectedLayer, SoftmaxLayer
test_data = net3.load_mydata_shared()
mini_batch_size = 10
net = Network([ ConvPoolLayer(image_shape=(mini_batch_size, 1, 166, 166),filter_shape=(5, 1, 5,5), poolsize=(2, 2)),
ConvPoolLayer(image_shape=(mini_batch_size, 5, 81, 81),filter_shape=(10, 5, 6,6), poolsize=(2, 2)),
ConvPoolLayer(image_shape=(mini_batch_size, 10, 38, 38),filter_shape=(15, 10, 5, 5 ),poolsize=(2, 2)),
ConvPoolLayer(image_shape=(mini_batch_size, 15, 17, 17),filter_shape=(20, 15, 4, 4 ),poolsize=(2, 2)),
ConvPoolLayer(image_shape=(mini_batch_size, 20, 7, 7),filter_shape=(40, 20, 2, 2 ),poolsize=(2, 2)),
FullyConnectedLayer(n_in=40*3*3, n_out=100),SoftmaxLayer(n_in=100, n_out=3)], mini_batch_size)
#print net.layers[-1].y_out.eval()
#x2 = net.layers[-1].y_out
#y2 = T.cast(x2, 'int32')
#print (pp(net.layers[-1].y_out))
#help(T.argmax)
#print net.layers[-1].y_out.shape.eval()
#net.SGD( 2, mini_batch_size, 0.03 ,test_data)
加载.mat形式的单张测试图片代码如下:
"""
mnist_loader
~~~~~~~~~~~~
A library to load the MNIST image data. For details of the data
structures that are returned, see the doc strings for ``load_data``
and ``load_data_wrapper``. In practice, ``load_data_wrapper`` is the
function usually called by our neural network code.
"""
#### Libraries
# Standard library
import cPickle
import gzip
# Third-party libraries
import numpy as np
import scipy.io as sio
from random import shuffle
def load_data():
matx = sio.loadmat('exact_one_x.mat')
datax = matx['X']
datax=datax.transpose()
print datax.shape
maty = sio.loadmat('one_y.mat')
datay = maty['M']
datay=datay.transpose()
print datay.shape
test_data = (datax,datay[0])
return ( test_data)
def load_data_wrapper():
"""Return a tuple containing ``(training_data, validation_data,
test_data)``. Based on ``load_data``, but the format is more
convenient for use in our implementation of neural networks.
In particular, ``training_data`` is a list containing 50,000
2-tuples ``(x, y)``. ``x`` is a 784-dimensional numpy.ndarray
containing the input image. ``y`` is a 10-dimensional
numpy.ndarray representing the unit vector corresponding to the
correct digit for ``x``.
``validation_data`` and ``test_data`` are lists containing 10,000
2-tuples ``(x, y)``. In each case, ``x`` is a 784-dimensional
numpy.ndarry containing the input image, and ``y`` is the
corresponding classification, i.e., the digit values (integers)
corresponding to ``x``.
Obviously, this means we're using slightly different formats for
the training data and the validation / test data. These formats
turn out to be the most convenient for use in our neural network
code."""
tr_d, va_d, te_d = load_data()
training_inputs = [np.reshape(x, (27556, 1)) for x in tr_d[0]]
training_results = [vectorized_result(y) for y in tr_d[1]]
training_data = zip(training_inputs, training_results)
shuffle(training_data)
validation_inputs = [np.reshape(x, (27556, 1)) for x in va_d[0]]
validation_data = zip(validation_inputs, va_d[1])
test_inputs = [np.reshape(x, (27556, 1)) for x in te_d[0]]
test_data = zip(test_inputs, te_d[1])
return (training_data, validation_data, test_data)
def vectorized_result(j):
"""Return a 10-dimensional unit vector with a 1.0 in the jth
position and zeroes elsewhere. This is used to convert a digit
(0...9) into a corresponding desired output from the neural
network."""
e = np.zeros((3, 1))
e[j] = 1.0
return e
最佳答案
您可以使用已经在您的代码中定义的 test_mb_predictions 函数。您只需要在测试代码中加载单个图像,批量大小为 1(而不是 10),并打印 test_mb_predictions 返回的类。
关于python - 使用经过训练的 ConvNet 的参数预测图像类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35726182/
我正在使用 ConvNet 构建模型来进行天气预报。我的输入数据是 96x144 矩阵(代表地理区域)的 10K 个样本,网格中每个点在固定高度处具有变量 Z(位势高度)的值。如果我包含 3 个不同的
我一直在编码 this example TensorFlow 中的卷积网络,我对这种权重分配感到困惑: weights = { # 5x5 conv, 1 input, 32 outputs 'wc1
我正在尝试使用卷积神经网络对图像进行分类。我经历过这个 tutorial关于深度学习并实现了给定的 code有很多修改。我添加了更多的卷积层和最大池化层,并更改了输入以接受 166x166 的输入。为
我目前正在尝试使用在 Imagenet 上训练的 Densenet 进行迁移学习,以输出一个有序整数值 {2 < 3 < 4 < 5 < 6}。我使用 this method 将目标变量编码为长度为
我正在尝试仅使用 numpy 实现CNN。 在进行反向传播时,我发现我必须使用 col2im 来 reshape dx,所以我检查了 https://github.com/huyouare/CS231
我正在尝试使用 VGG16 网络进行图像分类。我尝试了两种不同的方法来做到这一点,据我所知,这两种方法应该大致相同,但结果却截然不同。 方法 1: 使用 VGG16 提取特征,并使用自定义全连接网络拟
我在一篇论文中读到了这一点:“我们不是在第一个卷积层中使用相对较大的感受野,而是在整个网络中使用非常小的 3 × 3 感受野,这些感受野在每个网络中与输入进行卷积。像素(步幅为 1)。很容易看出,两个
我正在使用卷积网络来预测时间序列。为此,我使用滚动窗口来获取最后 t 点,将它们用作时间序列。每个功能都将成为一个 channel ,因此我设置了多个时间序列。数据需要为 3 维 [n_samples
我正在尝试创建一个用于图像分割的简单 3D U-net,只是为了学习如何使用图层。因此,我进行步幅为 2 的 3D 卷积,然后进行转置反卷积以获得相同的图像大小。我也过度拟合了一个小集(测试集)只是为
我正在完成 Stanford's cs231n course 的作业靠我自己。我不是该类(class)的学生。我在their subreddit中问了同样的问题,但似乎没有人在那里。希望能在这里找到一
我目前正在构建一个卷积神经网络来区分清晰的 ECG 图像和有噪声的 ECG 图像。 有噪音: 没有噪音: 我的问题 所以我确实在 tensorflow 之上使用 keras 构建了一个 convnet
我正在尝试在 Excel 中构建一个非常简单的卷积神经网络。该模型是一个图像分类器,试图识别手写的 I、O 和 X;并使用 Keras 对 EMNIST 字母数据集的一个子集进行了训练。 Excel
我正在尝试构建一个简单的卷积神经网络,将时间序列分为六类之一。由于不兼容的形状错误,我在训练网络时遇到问题。 在以下代码中,n_feats = 1000,n_classes = 6。 Fs = 100
尝试使用 Python + Keras 训练 cnn(卷积神经网络)。但即使是最简单的问题似乎也很难回答,而且那里的教程也没有我寻求的答案。 我可以访问我想要识别的少数类别的数千张图像。但我该如何准备
我有一个在 convnet.js 中创建的神经网络模型,我必须使用 Keras 来定义它。有谁知道我该怎么做? neural = { net : new convnetjs.Net
我正在文本(字符级别)上训练卷积神经网络,并且我想要进行最大池化。 tf.nn.max_pool 需要一个 4 级张量,但 1-d 卷积网络在 tensorflow 中是 3 级([batch, wi
我修改了 MNIST (28x28) Convnet 教程代码以接受更大的图像 (150x150)。但是,当我尝试训练时,我收到此错误(完整堆栈跟踪请参见结尾): W tensorflow/core/
要解决的整个问题是读取这些能量计显示的数字。 An Image of the energy meter.然后我需要能够在 android 应用程序中实现所有这些。我要做的是首先通过回归找到包含数字的黑
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
我创建了一个微调网络,它使用 vgg16 作为基础。我正在关注 Deep Learning With Python 中的 5.4.2 可视化 CovNet 过滤器部分(这与 Keras 博客上的可视化
我是一名优秀的程序员,十分优秀!