gpt4 book ai didi

python - 在 Tensorflow 的 2D 数组中存储和标记图像

转载 作者:行者123 更新时间:2023-11-30 09:05:59 25 4
gpt4 key购买 nike

我想使用 Tensorflow 对三种不同的图像类别进行图像识别。我现在的问题是为我的训练集标记图像并将其存储在二维数组中以便在识别中使用它。我已经使用了一个方法来存储 2 个类(在代码示例中是 XY),但现在我也想为第三个类执行此操作(在代码中以Z命名。

import cv2                 # working with, mainly resizing, images
import numpy as np # dealing with arrays
import os # dealing with directories
from random import shuffle # mixing up current data
from tqdm import tqdm # percentage bar for tasks
import time
import matplotlib.pyplot as plt

import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression


TRAIN_DIR = 'MYPATH'
TEST_DIR = 'MYPATH'
IMG_SIZE = 80
# learning rate
LR = 1e-5

MODEL_NAME = 'name-{}-{}.model'.format(LR, '2conv-basic')

# convert image and label information to array information
def label_img(img):
#split images
word_label = img.split('.')[-3]
if word_label == 'X': return [1,0]
elif word_label == 'Y': return [0,1]
elif word_label == 'Z' : return [???]

# create training data array
def create_train_data():
training_data = []
for img in tqdm(os.listdir(TRAIN_DIR)):
label = label_img(img)
path = os.path.join(TRAIN_DIR,img)
img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))
training_data.append([np.array(img),np.array(label)])
shuffle(training_data)
np.save('train_data.npy', training_data)
return training_data

def process_test_data():
testing_data = []
for img in tqdm(os.listdir(TEST_DIR)):
path = os.path.join(TEST_DIR,img)
img_num = img.split('.')[1]
img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))
testing_data.append([np.array(img), img_num])

shuffle(testing_data)
np.save('test_data.npy', testing_data)
return testing_data

train_data = create_train_data()
# if you already have train data:
#train_data = np.load('train_data.npy')

import tensorflow as tf
tf.reset_default_graph()

convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')

convnet = conv_2d(convnet, 32, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)

convnet = conv_2d(convnet, 64, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)

convnet = conv_2d(convnet, 32, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)

convnet = conv_2d(convnet, 64, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)

convnet = conv_2d(convnet, 32, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)

convnet = conv_2d(convnet, 64, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)

convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)

convnet = fully_connected(convnet, 2, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')

model = tflearn.DNN(convnet, tensorboard_dir='log')

if os.path.exists('{}.meta'.format(MODEL_NAME)):
model.load(MODEL_NAME)
print('model loaded!')

train = train_data[:-500]
test = train_data[-500:]

X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE, IMG_SIZE, 1)
Y = [i[1] for i in train]

test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
test_y = [i[1] for i in test]

model.fit({'input': X}, {'targets': Y}, n_epoch=15, validation_set=({'input': test_x}, {'targets': test_y}),
snapshot_step=500, show_metric=True, run_id=MODEL_NAME)

model.save(MODEL_NAME)

# if you need to create the data:
test_data = process_test_data()
# if you already have some saved:
#test_data = np.load('test_data.npy')

fig=plt.figure()

for num,data in enumerate(test_data[:12]):

img_num = data[1]
img_data = data[0]

y = fig.add_subplot(3,4,num+1)
orig = img_data
data = img_data.reshape(IMG_SIZE,IMG_SIZE,1)
#model_out = model.predict([data])[0]
model_out = model.predict([data])[0]

if np.argmax(model_out) == 1: str_label='X'
else: str_label='Y'

y.imshow(orig,cmap='gray')
plt.title(str_label)
y.axes.get_xaxis().set_visible(False)
y.axes.get_yaxis().set_visible(False)
plt.show()

最佳答案

要添加类,只需扩展图像标签数组的维度即可:

# convert image and label information to array information
def label_img(img):
#split images
word_label = img.split('.')[-3]
if word_label == 'X': return [1,0,0]
elif word_label == 'Y': return [0,1,0]
elif word_label == 'Z' : return [0,0,1]

您还需要更新 softmax 分类器来处理 3 个类别:

convnet = fully_connected(convnet, 3, activation='softmax')

您还需要禁用旧模型的加载。旧模型仅对旧图有效,但由于它正在改变,我们必须从头开始。

###
if os.path.exists('{}.meta'.format(MODEL_NAME)):
model.load(MODEL_NAME)
print('model loaded!')
###

关于python - 在 Tensorflow 的 2D 数组中存储和标记图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51884439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com