diff --git a/.idea/http1.iml b/.idea/http1.iml index d0876a7..da03a89 100644 --- a/.idea/http1.iml +++ b/.idea/http1.iml @@ -1,8 +1,10 @@ - - + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index b7ea289..82e31b1 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + diff --git a/IROCMdjango/IROCM/views.py b/IROCMdjango/IROCM/views.py index 533be68..8771d0c 100644 --- a/IROCMdjango/IROCM/views.py +++ b/IROCMdjango/IROCM/views.py @@ -7,6 +7,7 @@ import os # Create your views here. + def index(request): return render(request,'index.html',{}) @@ -31,16 +32,151 @@ def upload(request): # 多次写入 for i in rev_file.chunks(): f.write(i) - # 写完之后要关闭 f.close() print('上传成功') + str = predict_image(file_path) + print(str) # 返回 - return render(request, 'results.html', {}) + return render(request, 'results.html', {"data":str}) except Exception as e: print('上传失败') print(e) return render(request, 'upload.html', {}) else: print('其他请求') - return render(request, 'upload.html', {}) \ No newline at end of file + return render(request, 'upload.html', {}) + + + +import paddle +import numpy as np +from PIL import Image + +''' +参数配置 +''' +train_parameters = { + "input_size": [3, 224, 224], # 输入图片的shape + "class_dim": 5, # 分类数 + "label_dict": {'0': '百合', '1': '党参', '2': '枸杞', '3': '槐花', '4': '金银花'}, # 标签字典 +} + + +class ConvPool(paddle.nn.Layer): + """ 卷积+池化 """ + + def __init__(self, + num_channels, + num_filters, + filter_size, + pool_size, + pool_stride, + groups, + conv_stride=1, + conv_padding=1, + ): + super(ConvPool, self).__init__() + + for i in range(groups): + self.add_sublayer( # 添加子层实例 + 'bb_%d' % i, + paddle.nn.Conv2D( # layer + in_channels=num_channels, # 通道数 + out_channels=num_filters, # 卷积核个数 + kernel_size=filter_size, # 卷积核大小 + stride=conv_stride, # 步长 + padding=conv_padding, # padding + ) + ) + self.add_sublayer( + 'relu%d' % i, + paddle.nn.ReLU() + ) + num_channels = num_filters + + self.add_sublayer( + 'Maxpool', + paddle.nn.MaxPool2D( + kernel_size=pool_size, # 池化核大小 + stride=pool_stride # 池化步长 + ) + ) + + def forward(self, inputs): + x = inputs + for prefix, sub_layer in self.named_children(): + # print(prefix,sub_layer) + x = sub_layer(x) + return x + + +class VGGNet(paddle.nn.Layer): + + def __init__(self): + super(VGGNet, self).__init__() + self.convpool01 = ConvPool( + 3, 64, 3, 2, 2, 2) # 3:通道数,64:卷积核个数,3:卷积核大小,2:池化核大小,2:池化步长,2:连续卷积个数 + self.convpool02 = ConvPool( + 64, 128, 3, 2, 2, 2) + self.convpool03 = ConvPool( + 128, 256, 3, 2, 2, 3) + self.convpool04 = ConvPool( + 256, 512, 3, 2, 2, 3) + self.convpool05 = ConvPool( + 512, 512, 3, 2, 2, 3) + self.pool_5_shape = 512 * 7 * 7 + self.fc01 = paddle.nn.Linear(self.pool_5_shape, 4096) + self.fc02 = paddle.nn.Linear(4096, 4096) + self.fc03 = paddle.nn.Linear(4096, train_parameters['class_dim']) + + def forward(self, inputs, label=None): + # print('input_shape:', inputs.shape) #[8, 3, 224, 224] + """前向计算""" + out = self.convpool01(inputs) + # print('convpool01_shape:', out.shape) #[8, 64, 112, 112] + out = self.convpool02(out) + # print('convpool02_shape:', out.shape) #[8, 128, 56, 56] + out = self.convpool03(out) + # print('convpool03_shape:', out.shape) #[8, 256, 28, 28] + out = self.convpool04(out) + # print('convpool04_shape:', out.shape) #[8, 512, 14, 14] + out = self.convpool05(out) + # print('convpool05_shape:', out.shape) #[8, 512, 7, 7] + + out = paddle.reshape(out, shape=[-1, 512 * 7 * 7]) + out = self.fc01(out) + out = self.fc02(out) + out = self.fc03(out) + + if label is not None: + acc = paddle.metric.accuracy(input=out, label=label) + return out, acc + else: + return out + +# 预测图片预处理 +def load_image(img_path): + img = Image.open(img_path) + if img.mode != 'RGB': + img = img.convert('RGB') + img = img.resize((224, 224), Image.BILINEAR) + img = np.array(img).astype('float32') + img = img.transpose((2, 0, 1)) / 255 # HWC to CHW 及归一化 + return img + +def predict_image(file_path): + label_dic = train_parameters['label_dict'] + + # 加载模型 + model__state_dict = paddle.load('D:/aistudio/work/checkpoints/save_dir_final.pdparams') + model_predict = VGGNet() + model_predict.set_state_dict(model__state_dict) + + infer_img = load_image(file_path) + infer_img = infer_img[np.newaxis, :, :, :] # reshape(-1,3,224,224) + infer_img = paddle.to_tensor(infer_img) + result = model_predict(infer_img) + lab = np.argmax(result.numpy()) + print("样本: {},被预测为:{}".format(file_path, label_dic[str(lab)])) + return label_dic[str(lab)] diff --git a/IROCMdjango/static/media/upimg.jpg b/IROCMdjango/static/media/upimg.jpg index bf0dffe..43c5c83 100644 Binary files a/IROCMdjango/static/media/upimg.jpg and b/IROCMdjango/static/media/upimg.jpg differ diff --git a/IROCMdjango/templates/IROCM/results.html b/IROCMdjango/templates/IROCM/results.html index dc63e1d..83693fe 100644 --- a/IROCMdjango/templates/IROCM/results.html +++ b/IROCMdjango/templates/IROCM/results.html @@ -89,7 +89,7 @@
药材信息
-

中文名:白术

+

中文名:{{ data }}

产地生境:分布中国江苏、浙江、福建、江西、安徽、四川、湖北及湖南等地,有栽培,但在江西、湖南、浙江、四川有野生,野生于山坡草地及山坡林下。模式标本采自日本的栽培类型。但日本无野生类型。日本的白术是十八世纪由中国引入作生药栽培的。

生长习性:白术喜凉爽气候,怕高温高湿环境,对土壤要求不严格,但以排水良好、土层深厚的微酸、碱及轻黏土为好。 平原地区要选土质疏松、肥力中等的地块。土壤过肥,幼苗生长过旺,易当年抽薹开花,影响药用质量。在山区可选择土层较厚,有一定坡度的土地种植。前茬最好是禾本科作物,不宜选择烟草、花生、油菜等作物茬,否则易发生病害。

diff --git a/MultiClass.py b/MultiClass.py index 0a1579a..c018f43 100644 --- a/MultiClass.py +++ b/MultiClass.py @@ -15,7 +15,7 @@ from paddle.io import Dataset train_parameters = { "input_size": [3, 224, 224], # 输入图片的shape "class_dim": -1, # 分类数 - "src_path": "D:/aistudio/data/data55190/Chinese Medicine.zip", # 原始数据集路径 + "src_path": "D:/aistudio/data/dataset/Chinese Medicine.zip", # 原始数据集路径 "target_path": "D:/aistudio/data/", # 要解压的路径 "train_list_path": "D:/aistudio/data/train.txt", # train.txt路径 "eval_list_path": "D:/aistudio/data/eval.txt", # eval.txt路径 @@ -400,7 +400,7 @@ def load_image(img_path): return img -infer_src_path = 'D:/aistudio/data/data55194/Chinese Medicine Infer.zip' +infer_src_path = 'D:/aistudio/data/dataset/Chinese Medicine Infer.zip' infer_dst_path = 'D:/aistudio/data/' unzip_infer_data(infer_src_path,infer_dst_path)