前言
隨著人工智能的不斷發(fā)展,深度學(xué)習(xí)這門技術(shù)也越來(lái)越重要,很多人都開(kāi)啟了學(xué)習(xí)機(jī)器學(xué)習(xí),本文將通過(guò)項(xiàng)目開(kāi)發(fā)實(shí)例,帶領(lǐng)大家從零開(kāi)始設(shè)計(jì)實(shí)現(xiàn)一款基于深度學(xué)習(xí)的圖像識(shí)別算法。
學(xué)習(xí)本章內(nèi)容, 你需要掌握以下基礎(chǔ)知識(shí):
- Python 基礎(chǔ)語(yǔ)法
- 計(jì)算機(jī)視覺(jué)庫(kù)(OpenCV)
- 深度學(xué)習(xí)框架(TensorFlow)
- 卷積神經(jīng)網(wǎng)絡(luò)(CNN)
一、基礎(chǔ)知識(shí)介紹
-
Python
Python 是一個(gè)高層次的結(jié)合了解釋性、編譯性、互動(dòng)性和面向?qū)ο蟮哪_本語(yǔ)言。學(xué)習(xí)鏈接:Python學(xué)習(xí) -
OpenCV
OpenCV 是一個(gè)開(kāi)源的跨平臺(tái)計(jì)算機(jī)視覺(jué)庫(kù)。實(shí)現(xiàn)了圖像處理和計(jì)算機(jī)視覺(jué) 方面的很多通用算法。在本設(shè)計(jì)中它主要集中在圖像的采集以及圖像預(yù)處理等功能。學(xué)習(xí)鏈接:OpenCV學(xué)習(xí) -
TensorFlow
TensorFlow 是谷歌開(kāi)源的計(jì)算框架,TensorFlow 框架可以很好地支持深度學(xué) 習(xí)的各種算法。本課設(shè)圖像識(shí)別部分采用的深度學(xué)習(xí)(deep learning)算法就是 用 TensorFlow 框架實(shí)現(xiàn)的。學(xué)習(xí)鏈接:TensorFlow學(xué)習(xí) -
CNN
卷積神經(jīng)網(wǎng)絡(luò)(Convolutional Neural Networks, CNN)是一類包含卷積計(jì)算 且具有深度結(jié)構(gòu)的前饋神經(jīng)網(wǎng)絡(luò)(Feedforward Neural Networks),是深度學(xué)習(xí)(deep learning)的代表算法之一。學(xué)習(xí)鏈接:CNN學(xué)習(xí)
二、數(shù)據(jù)集收集
在進(jìn)行圖像識(shí)別前,首先需要收集數(shù)據(jù)集(數(shù)據(jù)集下載),其次對(duì)于數(shù)據(jù)集做預(yù)處理,然后才能通過(guò)深度卷積神經(jīng)網(wǎng)絡(luò)來(lái)進(jìn)行特征學(xué)習(xí),得到估計(jì)分類模型。對(duì)于數(shù)據(jù)集的要求,在卷積神經(jīng)網(wǎng)絡(luò)(CNN)中,由于對(duì)輸入圖像向量的權(quán)值參數(shù)的數(shù)量是固定的,所以在用卷積網(wǎng)絡(luò)(CNN)對(duì)數(shù)據(jù)集進(jìn)行模型訓(xùn)練前需要進(jìn)行圖像預(yù)處理,保證輸入的圖像尺寸是固定一致的。
本案例以實(shí)現(xiàn)垃圾識(shí)別分類作為最終實(shí)現(xiàn)目標(biāo), 收集數(shù)據(jù)集包含四類圖片,分別為廚余垃圾、可回收垃圾、有毒垃圾、其它垃圾,每類圖片數(shù)據(jù)集規(guī)模為200張(學(xué)習(xí)者可以根據(jù)自己需求選擇數(shù)據(jù)集類型及規(guī)模。數(shù)據(jù)集下載
數(shù)據(jù)集圖片預(yù)處理代碼如下:
#數(shù)據(jù)圖片rename
#數(shù)據(jù)集路徑:(self.image_path = "./picture/")
def rename(self):
listdir = os.listdir(self.image_path)
i = 0
while i < len(listdir):
images_list_dir = os.listdir(os.path.join(self.image_path, listdir[i]))
j = 0
while j < len(images_list_dir):
old_name = os.path.join(self.image_path, listdir[i], images_list_dir[j])
new_name = os.path.join(self.image_path, "%d-%d" % (i, j) + ".jpg")
os.rename(old_name, new_name)
j += 1
i += 1
for p in range(len(listdir)):
tmp_path = os.path.join(self.image_path, listdir[p])
if os.path.exists(tmp_path):
os.removedirs(tmp_path)
#圖片resize
def resize_img(self):
listdir = os.listdir(self.image_path)
for file in listdir:
file_path = os.path.join(self.image_path, file)
try:
imread = cv2.imread(file_path)
resize = cv2.resize(imread, (200, 200))
cv2.imwrite(os.path.join(self.image_path, file), resize)
except Exception:
os.remove(file_path)
continue
#轉(zhuǎn)存圖片信息到csv文件
#csv生成路徑:(csv_file_saved_path = "./picture/")
def train_data_to_csv(self):
files = os.listdir(self.image_path)
data = []
for file in files:
data.append({"path": self.image_path + file, "label": file[0]})
frame = pd.DataFrame(data, columns=['path', 'label'])
dummies = pd.get_dummies(frame['label'], 'label')
concat = pd.concat([frame, dummies], 1)
concat.to_csv(csv_file_saved_path + "train.csv")
三、模型訓(xùn)練
模型訓(xùn)練代碼如下:
#模型訓(xùn)練算法
def build_model():
with tf.name_scope("input"):
x = tf.placeholder(tf.float32, [None, 200, 200, 3], "x")
y = tf.placeholder(tf.float32, [None, 5], "y")
with tf.variable_scope("conv_layer_1"):
conv1 = tf.layers.conv2d(x, 64, [3, 3], activation=tf.nn.relu, name='conv1')
max1 = tf.layers.max_pooling2d(conv1, [2, 2], [2, 2])
bn1 = tf.layers.batch_normalization(max1, name='bn1')
output1 = tf.layers.dropout(bn1, name='droput')
with tf.variable_scope("conv_layer_2"):
conv2 = tf.layers.conv2d(output1, 64, [3, 3], activation=tf.nn.relu, name='conv2')
max2 = tf.layers.max_pooling2d(conv2, [2, 2], [2, 2], name='max2')
bn2 = tf.layers.batch_normalization(max2)
output2 = tf.layers.dropout(bn2, name='dropout')
with tf.variable_scope("conv_layer_3"):
conv3 = tf.layers.conv2d(output2, 64, [3, 3], activation=tf.nn.relu, name='conv3')
max3 = tf.layers.max_pooling2d(conv3, [2, 2], [2, 2], name='max3')
bn3 = tf.layers.batch_normalization(max3, name='bn3')
output3 = bn3
with tf.variable_scope("conv_layer_4"):
conv4 = tf.layers.conv2d(output3, 32, [3, 3], activation=tf.nn.relu, name='conv4')
max4 = tf.layers.max_pooling2d(conv4, [2, 2], [2, 2], name='max4')
bn4 = tf.layers.batch_normalization(max4, name='bn4')
output = bn4
flatten = tf.layers.flatten(output, 'flatten')
with tf.variable_scope("fc_layer1"):
fc1 = tf.layers.dense(flatten, 256, activation=tf.nn.relu)
fc_bn1 = tf.layers.batch_normalization(fc1, name='bn1')
dropout1 = tf.layers.dropout(fc_bn1, 0.5)
with tf.variable_scope("fc_layer2"):
fc2 = tf.layers.dense(dropout1, 128, activation=tf.nn.relu)
dropout2 = tf.layers.dropout(fc2)
with tf.variable_scope("fc_layer3"):
fc3 = tf.layers.dense(dropout2, 64)
dropout3 = tf.layers.dropout(fc3)
with tf.variable_scope("fc_layer4"):
fc4 = tf.layers.dense(dropout3, 32)
with tf.variable_scope("fc_layer5"):
fc5 = tf.layers.dense(fc4, 5)
softmax = tf.nn.softmax(fc5, name='softmax')
predict = tf.argmax(softmax, axis=1)
loss = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits_v2(logits=fc5, labels=y, name='loss'))
tf.summary.scalar("loss", loss)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predict, tf.argmax(y, axis=1)), tf.float32))
tf.summary.scalar("acc", accuracy)
merged = tf.summary.merge_all()
return x, y, predict, loss, accuracy, merged, softmax
#模型訓(xùn)練
def train(self):
saver = tf.train.Saver(max_to_keep=3)
sess = tf.InteractiveSession(config=self.config)
sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter("./log", graph=sess.graph)
for i in range(100):
j = 1
all_loss_ = 0
all_acc_ = 0
while j <= self.batches and self.has_next_batch:
train_x, train_y = self.next_batch()
_, loss_, accuracy_, merged_ = sess.run([ self.opt, self.loss, self.accuracy, self.merged ],
feed_dict={self.x: train_x, self.y: train_y})
all_loss_ += loss_
all_acc_ += accuracy_
print("\repoch %d-- batch: %d--> " % (i, j) + "=" * j + ">" + "-" * (
self.batches - j) + "\t\t loss: %.4f, acc: %.4f" % (
loss_, accuracy_), )
j += 1
writer.add_summary(merged_, i * self.batches + j - 1)
print("\n===epoch %d=== > mean loss is : %.4f, mean acc is : %.4f" % (
i, all_loss_ / self.batches, all_acc_ / self.batches))
test_x, test_y = self.get_test_data()
test_loss_, test_acc_ = sess.run([ self.loss, self.accuracy ],
feed_dict={self.x: test_x[ 0:16 ], self.y: test_y[ 0:16 ]})
print("===epoch %d=== > test loss is : %.4f, test acc is : %.4f" % (
i, test_loss_, test_acc_))
self.start = 0
self.has_next_batch = True
if i % 5 == 0:
saver.save(sess, "./h5_dell/mode.ckpt", i)
sess.close()
四、圖像識(shí)別分類
圖像識(shí)別分類代碼:
#利用模型實(shí)時(shí)識(shí)別圖像
def predict_value(self, type='image', image_path=None):
saver = tf.train.Saver()
sess = tf.InteractiveSession()
saver.restore(sess, tf.train.latest_checkpoint("./h5_dell1/"))
if type == 'image':
assert image_path is not None
image = cv2.imread(image_path)
image = cv2.resize(image, (200, 200))
image = np.asarray(image, np.float32) / 255.
image = np.reshape(image, (1, image.shape[ 0 ], image.shape[ 1 ], image.shape[ 2 ]))
[ predict, probab ] = sess.run([ self.predict, self.probab ], feed_dict={self.x: image})
# predict = sess.run(self.predict, feed_dict={self.x: image})
# print("what? 1:",np.max(probab))
# print("what? 2:",predict[0])
return predict[0]
if (np.max(probab)<1):
print("recognise fail")
predict=4
print(predict)
elif type == 'video':
capture = cv2.VideoCapture(0)
while True:
ret, frame = capture.read()
resize = cv2.resize(frame, (200, 200))
x_ = np.asarray(resize, np.float32) / 255.
x_ = np.reshape(x_, [ 1, x_.shape[ 0 ], x_.shape[ 1 ], x_.shape[ 2 ] ])
[ predict, probab ] = sess.run([ self.predict, self.probab ], feed_dict={self.x: x_})
if predict == 0:
cv2.putText(frame, "0 probab: %.3f" % np.max(probab), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,
(0, 0, 255), 2, cv2.LINE_AA)
elif predict == 1:
cv2.putText(frame, "1 probab: %.3f" % np.max(probab), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,
(0, 255, 255), 2, cv2.LINE_AA)
elif predict == 2:
cv2.putText(frame, "2 probab: %.3f" % np.max(probab), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,
(0, 255, 0), 2, cv2.LINE_AA)
elif predict == 3:
cv2.putText(frame, "3 probab: %.3f" % np.max(probab), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,
(255, 0, 255), 2, cv2.LINE_AA)
elif predict == 4:
cv2.putText(frame, "4 probab: %.3f" % np.max(probab), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,
(255, 0, 255), 2, cv2.LINE_AA)
if predict==3:
print("1111")
print(predict)
cv2.imshow("recognized", frame)
key = cv2.waitKey(1)
if key == 27:
break
cv2.destroyAllWindows()
capture.release()
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-779300.html
總結(jié)
本章節(jié)內(nèi)容以實(shí)際項(xiàng)目案例介紹神經(jīng)網(wǎng)絡(luò)圖像識(shí)別算法的搭建及使用詳細(xì)步驟,介 紹卷積神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)圖像識(shí)別分類的詳細(xì)過(guò)程,以及實(shí)現(xiàn)效果的展示,希望文章能夠幫助到大家,有問(wèn)題請(qǐng)點(diǎn)擊【鏈接】溝通交流, 獲取源碼。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-779300.html
到了這里,關(guān)于項(xiàng)目實(shí)戰(zhàn)解析:基于深度學(xué)習(xí)搭建卷積神經(jīng)網(wǎng)絡(luò)模型算法,實(shí)現(xiàn)圖像識(shí)別分類的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!