keras的plot_model错误三连以及torch配置

from keras.layers import Input, Conv2D, MaxPool2D, Flatten, Dense, Activation
from keras import regularizers
from keras.models import Model
import keras.activations as activation
from keras.utils import plot_model
import os
'''
原味Lenet,没有bn,dropout之类的处理
2020229
chenglong
'''

class Lenet():
    def __init__(self,data_size=(100,32,32,3),
                 n_classes=10):
        # 5
        if len(data_size) == 4:
            self.data_size = data_size
        else:
            raise TypeError
        self.n_classes = n_classes
        self.decay_weight = 0.5e-3
    @property
    def data_shape(self):
        return self.data_size[1:]
    def build(self):
        data_input=Input(batch_shape=self.data_size)
        net =Conv2D(6,(5,5),strides=(1,1),
                    padding="valid",activation="sigmoid",
                    kernel_regularizer=regularizers.l2(self.decay_weight),
                    name="c1")(data_input)
        # net = BatchNormalization()
        net = MaxPool2D((2,2),strides=(2,2),name="s2")(net)
        net = Conv2D(16,(5,5),strides=(1,1),
                     activation="relu",padding="same",
                     kernel_regularizer=regularizers.l2(self.decay_weight),
                     name="c3")(net)
        net = MaxPool2D((2,2),strides=(2,2),name="s4")(net)
        net = Flatten()(net)
        net = Dense(120,name="f5",activation="sigmoid")(net)
        net = Dense(84,activation="sigmoid",name="f6")(net)
        net = Dense(self.n_classes, activation="softmax")(net)
        net = Activation('softmax')(net)
        model = Model(inputs=data_input,outputs=net)
        plot_model(model, to_file=os.path.join('./', "base_model.png"), show_shapes=True)
        model.summary()
        return net


model = Lenet()
model.build()

 1 会报错,如下图。需要需要安装pydot和cython两个包。pip安装就行。

keras的plot_model错误三连以及torch配置_第1张图片

 2 安装完还会报错,如下图,没装Graphviz这个软件。keras的plot_model错误三连以及torch配置_第2张图片

 去官网,Graphviz,下载。

keras的plot_model错误三连以及torch配置_第3张图片

然后一直next就行了。然后把’D:\Program Files (x86)\Graphviz2.38\bin‘添加到环境变量的path。

3 然后会报这个错误

keras的plot_model错误三连以及torch配置_第4张图片

然后用everything找的pydot.py文件,然后把文件中的self.prog = 'dot'修改为self.prog = 'dot.exe'就行了。

然后运行,这个图不是上面代码的结果。

keras的plot_model错误三连以及torch配置_第5张图片

关于环境变量问题,需要重启电脑


pip install graphviz

pip install torchviz 
import torch
import torch.nn as nn

from torchviz import make_dot
from torch.autograd import Variable

class LeNet(nn.Module):
    def __init__(self):
        super(LeNet, self).__init__()
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.mp1 = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.mp2 = nn.MaxPool2d(2, 2)
        self.conv3 = nn.Conv2d(400, 120, 1, 1)
        self.fc4 = nn.Linear(120, 84)
        self.fc5 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = self.mp1(x)
        x = self.conv2(x)
        x = self.mp2(x)
        x = x.view(-1, x.size()[3] * x.size()[2] * x.size()[1])
        x = torch.unsqueeze(x, dim=-1)
        x = torch.unsqueeze(x, dim=-1)
        x = self.conv3(x)  # conv代替fc?
        x = x.view(-1, x.size()[3] * x.size()[2] * x.size()[1])
        x = self.fc4(x)
        return self.fc5(x)


if __name__ == '__main__':
    a = torch.randn(1, 1, 32, 32)
    m = LeNet()

    y = m(a)
    vis_graph = make_dot(y.mean(), params=dict(m.named_parameters()))
    vis_graph.view()
    # print(m)

和keras输出结果不一样

你可能感兴趣的:(keras,深度学习,python)