首页 > 解决方案 > 多类多标签简单模型

问题描述

我已经工作了几个星期来完成一个项目。这个项目看起来很简单,但是有很多活动的部分。所以 - 创建一个具有 4 个输入图像的简单 CNN 模型。图像是树木。图像是树的树干、树枝、叶子和树的根。- 有 8 种不同的树。- 目标向模型提供四个图像,并让模型检测它是哪棵树。- 我必须从谷歌图像创建原始数据(搜索)当我为一个学校项目解决这个问题时,我正在努力研究如何预处理热标签和多类的图像。然后确保我的模型架构是准确的。

# import the necessary packages
from keras.models import Sequential
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Activation
from keras.layers.core import Dropout
from keras.layers.core import Dense
from keras.layers import Flatten
from keras.layers import Input
from keras.models import Model
from keras.layers import Dense , LSTM, concatenate, Input, Flatten

# define four inputs ( Different photos of trees)
inputA = Input(shape=(308, 308, 1)) # Trunk 
inputB = Input(shape=(308, 308, 1)) # Branch
inputC = Input(shape=(308, 308, 1)) # Leafs
inputD = Input(shape=(308, 308, 1)) # Roots
 
# the first branch operates on the first input
a = Conv2D(16, (2, 2), activation='relu')(inputA)
a = Conv2D(16, (2, 2), activation='relu')(a)
a = Model(inputs=inputA, outputs=a)
 
# the second branch opreates on the second input
b = Conv2D(16, (2, 2), activation='relu')(inputB)
b = Conv2D(16, (2, 2), activation='relu')(b)
b = Model(inputs=inputB, outputs=b)

# the third branch opreates on the third input
c = Conv2D(16, (2, 2), activation='relu')(inputC)
c = Conv2D(16, (2, 2), activation='relu')(c)
c = Model(inputs=inputC, outputs=c)

# the fourth branch opreates on the fourth input
d = Conv2D(16, (2, 2), activation='relu')(inputD)
d = Conv2D(16, (2, 2), activation='relu')(d)
d = Model(inputs=inputD, outputs=d)
 
# combine the output of the four branches
combined = concatenate([a.output, b.output, c.output, d.output])
 
# apply a FC layer and then a regression prediction on the
# combined outputs
z = Dense(128, activation="relu")(combined)
z = Dense(4, activation="softmax")(z)
 
# our model will accept the inputs of the four branches and then output a single value
model = Model(inputs=[a.input, b.input, c.input, d.input], outputs=z)
model.summary()

model.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['accuracy'])

这就是我一直在尝试对预处理数据进行热标记的方式。

from tqdm import tqdm
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline

train_data = ""
test_data = ""

def one_hot_label(img):
	label = img.split(".")[0]
	if label == "Trunk";
		ohl = np.array([1,0,0,0])
	elif label == "Branch":
		ohl = np.array([0,1,0,0])
  elif label == "Leaf":
		ohl = np.array([0,0,1,0])
  elif label == "Root":
		ohl = np.array([0,0,0,1])  
	return ohl
def train_data_with_label();
	train_images = []
	for i in tqdm(os.listdir(train_data)):
		path = os.path.join(train_data, i)
		img = cv2.imread(path, cv2.IMREAD_COLOR)
		img = cv2.resize(img, (308, 308))
		train_images.append([np.array(img), one_hot_label(i)])
	shuffle(train_images)
	return train_images

def test_data_with_label();
	test_images = []
	for i in tqdm(os.listdir(test_data)):
		path = os.path.join(test_data, i)
		img = cv2.imread(path, cv2.IMREAD_COLOR)
		img = cv2.resize(img, (308, 308))
		test_images.append([np.array(img), one_hot_label(i)])
	shuffle(test_images)
	return test_images	

这是一个巨大的职位。我知道我一团糟,真的很挣扎。当您运行数据时,其准确性如此之低,我知道出了点问题。(约 18%)如果有人能帮忙!!拜托了。它不一定是确切的答案,即使方向是有帮助的。我很感谢这个网站和上面的人。

谢谢 Noob Coder Dyl

标签: pythonkerasartificial-intelligenceconv-neural-networkimage-recognition

解决方案


推荐阅读