网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

python的卷积函数 python卷积

python三维卷积可以用什么函数? matlab只要用convn

写了一个输入和卷积核dim=2是一样的(都是3)的卷积函数,可以试试多加一个for循环变成三维卷积

在厦门等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供做网站、成都网站设计 网站设计制作定制网站建设,公司网站建设,企业网站建设,品牌网站制作,全网营销推广,成都外贸网站制作,厦门网站建设费用合理。

def conv3D(image, filter):

'''

三维卷积

:param image: 输入,shape为 [h,w,c], c=3

:param filter:  卷积核,shape为 [x,y,z], z=3

:return:

'''

h, w, c = image.shape

x, y, z = filter.shape

height_new = h - x + 1  # 输出 h

width_new = w - y + 1  # 输出 w

image_new = np.zeros((height_new, width_new), dtype=np.float)

for i in range(height_new):

for j in range(width_new):

r = np.sum(image[i:i+x, j:j+x, 0] * filter[:,:,0])

g = np.sum(image[i:i+y, j:j+y, 1] * filter[:,:,1])

b = np.sum(image[i:i+z, j:j+z, 2] * filter[:,:,2])

image_new[i, j] = np.sum([r,g,b])

image_new = image_new.clip(0, 255)

image_new = np.rint(image_new).astype('uint8')

return image_new

怎样用python构建一个卷积神经网络?

用keras框架较为方便

首先安装anaconda,然后通过pip安装keras

1、#导入各种用到的模块组件

from __future__ import absolute_import

from __future__ import print_function

from keras.preprocessing.image import ImageDataGenerator

from keras.models import Sequential

from keras.layers.core import Dense, Dropout, Activation, Flatten

from keras.layers.advanced_activations import PReLU

from keras.layers.convolutional import Convolution2D, MaxPooling2D

from keras.optimizers import SGD, Adadelta, Adagrad

from keras.utils import np_utils, generic_utils

from six.moves import range

from data import load_data

import random

import numpy as np

np.random.seed(1024)  # for reproducibility

2、。#打乱数据

index = [i for i in range(len(data))]

random.shuffle(index)

data = data[index]

label = label[index]

print(data.shape[0], ' samples')

#label为0~9共10个类别,keras要求格式为binary class matrices,转化一下,直接调用keras提供的这个函数

label = np_utils.to_categorical(label, 10)

###############

#开始建立CNN模型

###############

#生成一个model

model = Sequential()

3、#第一个卷积层,4个卷积核,每个卷积核大小5*5。1表示输入的图片的通道,灰度图为1通道。

#border_mode可以是valid或者full,具体看这里说明:

#激活函数用tanh

#你还可以在model.add(Activation('tanh'))后加上dropout的技巧: model.add(Dropout(0.5))

model.add(Convolution2D(4, 5, 5, border_mode='valid',input_shape=(1,28,28)))

model.add(Activation('tanh'))

#第二个卷积层,8个卷积核,每个卷积核大小3*3。4表示输入的特征图个数,等于上一层的卷积核个数

4、全连接层,先将前一层输出的二维特征图flatten为一维的。

#Dense就是隐藏层。16就是上一层输出的特征图个数。4是根据每个卷积层计算出来的:(28-5+1)得到24,(24-3+1)/2得到11,(11-3+1)/2得到4

#全连接有128个神经元节点,初始化方式为normal

model.add(Flatten())

model.add(Dense(128, init='normal'))

model.add(Activation('tanh'))

#Softmax分类,输出是10类别

model.add(Dense(10, init='normal'))

model.add(Activation('softmax'))

#############

#开始训练模型

##############

#使用SGD + momentum

#model.compile里的参数loss就是损失函数(目标函数)

sgd = SGD(lr=0.05, decay=1e-6, momentum=0.9, nesterov=True)

model.compile(loss='categorical_crossentropy', optimizer=sgd,metrics=["accuracy"])

#调用fit方法,就是一个训练过程. 训练的epoch数设为10,batch_size为100.

#数据经过随机打乱shuffle=True。verbose=1,训练过程中输出的信息,0、1、2三种方式都可以,无关紧要。show_accuracy=True,训练时每一个epoch都输出accuracy。

#validation_split=0.2,将20%的数据作为验证集。

model.fit(data, label, batch_size=100, nb_epoch=10,shuffle=True,verbose=1,validation_split=0.2)

"""

#使用data augmentation的方法

#一些参数和调用的方法,请看文档

datagen = ImageDataGenerator(

featurewise_center=True, # set input mean to 0 over the dataset

samplewise_center=False, # set each sample mean to 0

featurewise_std_normalization=True, # divide inputs by std of the dataset

samplewise_std_normalization=False, # divide each input by its std

zca_whitening=False, # apply ZCA whitening

rotation_range=20, # randomly rotate images in the range (degrees, 0 to 180)

width_shift_range=0.2, # randomly shift images horizontally (fraction of total width)

height_shift_range=0.2, # randomly shift images vertically (fraction of total height)

horizontal_flip=True, # randomly flip images

vertical_flip=False) # randomly flip images

# compute quantities required for featurewise normalization

# (std, mean, and principal components if ZCA whitening is applied)

datagen.fit(data)

for e in range(nb_epoch):

print('-'*40)

print('Epoch', e)

print('-'*40)

print("Training...")

# batch train with realtime data augmentation

progbar = generic_utils.Progbar(data.shape[0])

for X_batch, Y_batch in datagen.flow(data, label):

loss,accuracy = model.train(X_batch, Y_batch,accuracy=True)

progbar.add(X_batch.shape[0], values=[("train loss", loss),("accuracy:", accuracy)] )

Python 中用于两个值卷积的函数是什么,我知道matlab 中是conv,Python中有预知对应的吗

全部用文件IO的话可以这样: matlab把所有参数输出到一个文件里,然后用system命令调python脚本。python脚本读文件做计算结果再写文件。最后matlab再读文件得到结果。 假设python脚本的用法是: python xxx.py in.txt out.txt 则matlab调用命令...


网站标题:python的卷积函数 python卷积
URL标题:http://bjjierui.cn/article/hjcecj.html

其他资讯