-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.py
65 lines (56 loc) · 2.2 KB
/
processor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*
import cv2
import numpy
from flyai.processor.base import Base
from flyai.processor.download import check_download
from PIL import Image
from path import DATA_PATH
from torchvision import transforms
'''
把样例项目中的processor.py件复制过来替换即可
'''
class Processor(Base):
'''
参数为csv中作为输入x的一条数据,该方法会被dataset.next_train_batch()
和dataset.next_validation_batch()多次调用。可在该方法中做数据增强
该方法字段与app.yaml中的input:->columns:对应
'''
def input_x(self, image_path):
path = check_download(image_path, DATA_PATH)
image = cv2.imread(path)
image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_CUBIC)
# cv2.imshow('img.png', image)
# cv2.waitKey()
x_data = numpy.array(image)
x_data = x_data.astype(numpy.float32)
x_data = numpy.multiply(x_data, 1.0 / 255.0)
return x_data
'''
参数为csv中作为输入y的一条数据,该方法会被dataset.next_train_batch()
和dataset.next_validation_batch()多次调用。
该方法字段与app.yaml中的output:->columns:对应
'''
def output_x(self, image_path):
path = check_download(image_path, DATA_PATH)
image = Image.open(path).convert('RGB')
self.transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
x_data = self.transform(image)
x_data = numpy.array(x_data)
return x_data
'''
参数为csv中作为输入x的一条数据,该方法会被dataset.next_train_batch()
和dataset.next_validation_batch()多次调用。评估的时候会调用该方法做数据处理
该方法字段与app.yaml中的input:->columns:对应
'''
def input_y(self, labels):
one_hot_label = numpy.zeros([4]) ##生成全0矩阵
one_hot_label[labels] = 1 ##相应标签位置置
return one_hot_label
'''
输出的结果,会被dataset.to_categorys(data)调用
'''
def output_y(self, data):
return numpy.argmax(data)