• 大小: 7.09MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-11-09
  • 语言: 其他
  • 标签:

资源简介

使用深度学习的pytorch框架实现对15种中草药的识别,其中包含自己创建的小型中草药图片数据集。。。

资源截图

代码片段和文件信息

import torch
import numpy as np
import torch.nn as nn
from torch.autograd import Variable
import torchvision.datasets as dsets
from torchvision import transforms
from torch.utils.data import Dataset DataLoader
from PIL import Image
import os
from skimage import io
root=‘/home/hbz/PycharmProjects/medicine/‘

num_epochs = 20
batch_size = 5
learning_rate = 1e-3
correct = 0
total = 0
correct1 = 0
total1 = 0

img_transforms = transforms.Compose([
    transforms.Scale(28)
    transforms.RandomHorizontalFlip()
    transforms.CenterCrop(28)
    transforms.ToTensor()
    transforms.Normalize([0.5 0.5 0.5] [0.5 0.5 0.5])
])

test_transforms = transforms.Compose([
    transforms.Scale(28)
    transforms.CenterCrop(28)
    transforms.ToTensor()
    transforms.Normalize([0.5 0.5 0.5] [0.5 0.5 0.5])
])

img_transforms_2 = transforms.Compose([
    transforms.Scale(400)
    transforms.RandomHorizontalFlip()
    transforms.CenterCrop(320)
    transforms.ToTensor()
    transforms.Normalize([0.5 0.5 0.5] [0.5 0.5 0.5])
])

# #读取文件
def get_files(directory):
    return [os.path.join(directory f) for f in sorted(list(os.listdir(directory)))
            if os.path.isfile(os.path.join(directory f))]
#
#
file = get_files(‘/home/hbz/PycharmProjects/medicine/train‘)
f=open(root+‘train.txt‘‘w‘)
for i item in enumerate(file):
    if(i+1<=14):
            t=0
    elif(i+1<=28):
            t=1
    elif(i+1<=42):
            t=2
    elif(i+1<=56):
            t=3
    elif(i+1<=70):
            t=4
    elif(i+1<=84):
            t=5
    elif(i+1<=98):
            t=6
    elif(i+1<=112):
            t=7
    elif(i+1<=126):
            t=8
    elif(i+1<=140):
            t=9
    elif(i+1<=154):
            t=10
    print(‘Processing %i of %i (%s) %i‘ % (i+1 len(file) itemt))
    f.write(item+‘ ‘+str(t)+‘\n‘)
    #image = transform(Image.open(item).convert(‘L‘))
    #images = np.append(images image.numpy())

f.close()
#
file = get_files(‘/home/hbz/PycharmProjects/medicine/test‘)
f=open(root+‘test.txt‘‘w‘)
for i item in enumerate(file):
    if(i+1<=1):
            t=0
    elif(i+1<=2):
            t=1
    elif(i+1<=3):
            t=2
    elif(i+1<=4):
            t=3
    elif(i+1<=5):
            t=4
    elif(i+1<=6):
            t=5
    elif(i+1<=7):
            t=6
    elif(i+1<=8):
            t=7
    elif(i+1<=9):
            t=8
    elif(i+1<=10):
            t=9
    elif(i+1<=11):
            t=10
    print(‘Processing %i of %i (%s) %i‘ % (i+1 len(file) itemt))
    f.write(item+‘ ‘+str(t)+‘\n‘)
    #image = transform(Image.open(item).convert(‘L‘))
    #images = np.append(images image.numpy())

f.close()




# -----------------ready the dataset--------------------------
def default_loader(path):
    return Image.open(path).convert(‘RGB‘)

def Visualize_image(path):
    img=Image.open(path).convert(‘RGB‘)
    img=img_transforms_2(img)
    img2 = transforms.ToPILImage()(img).convert(‘RGB‘)
    img2.show()

Visualize_image(‘/home/hbz/imag

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-04-15 14:46  test\
     文件       35338  2018-04-15 08:29  test\image120.jpg
     文件       36291  2018-04-15 08:23  test\image165.jpg
     文件       35868  2018-04-15 08:21  test\image150.jpg
     文件       32968  2018-04-15 08:30  test\image135.jpg
     文件       12023  2018-04-15 08:26  test\image105.jpg
     文件       21763  2018-04-15 08:18  test\image090.jpg
     文件       62449  2018-04-15 08:10  test\image075.jpg
     文件       83818  2018-04-15 08:07  test\image060.jpg
     文件       49711  2018-04-15 08:05  test\image045.jpg
     文件       93452  2018-04-15 08:02  test\image030.jpg
     文件       38688  2018-04-14 23:58  test\image015.jpg
     目录           0  2018-04-15 14:46  train\
     文件       49724  2018-04-15 08:11  train\image072.jpg
     文件       36156  2018-04-15 08:14  train\image071.jpg
     文件      113198  2018-04-15 08:07  train\image059.jpg
     文件       46600  2018-04-15 08:09  train\image058.jpg
     文件       22851  2018-04-15 08:09  train\image057.jpg
     文件       72062  2018-04-15 08:08  train\image056.jpg
     文件       55752  2018-04-15 08:05  train\image044.jpg
     文件       67813  2018-04-15 08:06  train\image043.jpg
     文件       79315  2018-04-15 08:06  train\image042.jpg
     文件       52708  2018-04-15 08:06  train\image041.jpg
     文件       54083  2018-04-15 08:04  train\image029.jpg
     文件       35371  2018-04-15 08:02  train\image028.jpg
     文件       22121  2018-04-15 08:04  train\image027.jpg
     文件       34535  2018-04-15 08:04  train\image026.jpg
     文件       41306  2018-04-15 07:59  train\image014.jpg
     文件       35411  2018-04-14 23:58  train\image013.jpg
     文件      155056  2018-04-15 08:00  train\image012.jpg
     文件       63938  2018-04-15 08:00  train\image011.jpg
............此处省略139个文件信息

评论

共有 条评论

相关资源