赞
踩
新建一个文件夹AlexNet
,在文件夹AlexNet
新建一个文件夹flower_data
,将下载后的数据解压并放到文件夹flower_data
。
下载 Tensorflow 的花朵图片
http://download.tensorflow.org/example_images/flower_photos.tgz
在文件夹AlexNet
右键打开终端
gedit spile_data.py # 将 spile_data.py 拷入保存关闭
python spile_data.py # 运行 spile_data.py
spile_data.py
import os from shutil import copy import random def mkfile(file): if not os.path.exists(file): os.makedirs(file) file = 'flower_data/flower_photos' flower_class = [cla for cla in os.listdir(file) if ".txt" not in cla] mkfile('flower_data/train') for cla in flower_class: mkfile('flower_data/train/'+cla) mkfile('flower_data/val') for cla in flower_class: mkfile('flower_data/val/'+cla) split_rate = 0.1 for cla in flower_class: cla_path = file + '/' + cla + '/' images = os.listdir(cla_path) num = len(images) eval_index = random.sample(images, k=int(num*split_rate)) for index, image in enumerate(images): # 划分为验证集 if image in eval_index: image_path = cla_path + image new_path = 'flower_data/val/' + cla copy(image_path, new_path) # 划分为训练集 else: image_path = cla_path + image new_path = 'flower_data/train/' + cla copy(image_path, new_path) print("\r[{}] processing [{}/{}]".format(cla, index+1, num), end="") # processing bar print() print("processing done!")
在文件夹AlexNet
右键打开终端
gedit model.py # 将 model.py 拷入保存关闭
model.py
import torch.nn as nn import torch class AlexNet(nn.Module): def __init__(self, num_classes=5, init_weights=False): super(AlexNet, self).__init__() self.features = nn.Sequential( #打包 nn.Conv2d(3, 48, kernel_size=11, stride=4, padding=2), # input[3, 224, 224] output[48, 55, 55] 自动舍去小数点后 nn.ReLU(inplace=True), #inplace 可以载入更大模型 nn.MaxPool2d(kernel_size=3, stride=2), # output[48, 27, 27] kernel_num为原论文一半 nn.Conv2d(48, 128, kernel_size=5, padding=2), # output[128, 27, 27] nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2), # output[128, 13, 13] nn.Conv2d(128, 192, kernel_size=3, padding=1), # output[192, 13, 13] nn.ReLU(inplace=True), nn.Conv2d(192, 192, kernel_size=3, padding=1), # output[192, 13, 13] nn.ReLU(inplace=True), nn.Conv2d(192, 128, kernel_size=3, padding=1), # output[128, 13, 13] nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2), # output[128, 6, 6] ) self.classifier = nn.Sequential( nn.Dropout(p=0.5), #全链接 nn.Linear(128 * 6 * 6, 2048), nn.ReLU(inplace=True), nn.Dropout(p=0.5), nn.Linear(2048, 2048), nn.ReLU(inplace=True), nn.Linear(2048, num_classes), ) if init_weights: self._initialize_weights() def forward(self, x): x = self.features(x) x = torch.flatten(x, start_dim=1) #展平 或者view() x = self.classifier(x) return x def _initialize_weights(self): for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') #何教授方法 if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) #正态分布赋值 nn.init.constant_(m.bias, 0)
在文件夹AlexNet
右键打开终端
import torch
import torch.nn as nn
from torchvision import transforms, datasets, utils
import matplotlib.pyplot as plt
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。