使用jupyter實現貓和狗的分類
這篇部落格是參照一個pycharm來寫的,其實改的也不多,但是自己跑出來了
主要包括,讀取資料,定義模型,訓練,儲存模型,提取模型進行分類
1,訓練資料的讀取
#x訓練資料的讀取
import tensorflow as tf
import numpy as np
import os
獲取檔案,從一個特定的目錄中獲取
#獲取檔案的路徑
def get_files(file_dir):
cats=[]
label_cats=[]
dogs=[]
label_dogs=[]
#載入路徑並且寫入標籤值
for file in os.listdir(file_dir):
name=file.split(sep='.')
if name[0]=='cat':
cats.append(file_dir+file)
label_cats.append(0)
else:
dogs.append(file_dir+file)
label_dogs.append(1)
print("There are %d cats\nThere are %d dogs" % (len(cats), len(dogs)))
#打亂檔案的順序
image_list=np.hstack((cats,dogs))#將貓和狗的圖片整個一起
label_list=np.hstack((label_cats,label_dogs))
temp=np.array([image_list,label_list])
temp=temp.transpose()
np.random.shuffle(temp)#使用這種方式將他們打亂
image_list=list(temp[:,0])#第一列是影象
label_list=list(temp[:,1])#第二列是標籤
label_list=[int(i) for i in label_list]
return image_list,label_list
#定義生成批次的函式
#生成大小相同的批次
def get_batch(image,label,image_W,image_H,batch_size,capacity):
# image, label: 要生成batch的影象和標籤list
# image_W, image_H: 圖片的寬高
# batch_size: 每個batch有多少張圖片
# capacity: 佇列容量
# return: 影象和標籤的batch
# 將python.list型別轉換成tf能夠識別的格式
image=tf.cast(image,tf.string)
label=tf.cast(label,tf.int32)
#生成佇列
input_queue=tf.train.slice_input_producer([image,label])
image_contents=tf.read_file(input_queue[0])
label=input_queue[1]
image=tf.image.decode_jpeg(image_contents,channels=3)#
# 統一圖片大小read_file()讀取圖片之後,要按照圖片格式進行解碼。本例程中訓練資料是jpg格式的,所以使用decode_jpeg()解碼器,如果是其他格式,就要用其他解碼器,具
# 視訊方法
# image = tf.image.resize_image_with_crop_or_pad(image, image_W, image_H)
# 我的方法
image = tf.image.resize_images(image, [image_H, image_W], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
image = tf.cast(image, tf.float32)
# image = tf.image.per_image_standardization(image) # 標準化資料
image_batch, label_batch = tf.train.batch([image, label],
batch_size=batch_size,
num_threads=64, # 執行緒
capacity=capacity)
# 這行多餘?
# label_batch = tf.reshape(label_batch, [batch_size])
return image_batch, label_batch
2定義模型,就是定義一個網路結構
#生成大小相同的批次
def get_batch(image,label,image_W,image_H,batch_size,capacity):
# image, label: 要生成batch的影象和標籤list
# image_W, image_H: 圖片的寬高
# batch_size: 每個batch有多少張圖片
# capacity: 佇列容量
# return: 影象和標籤的batch
# 將python.list型別轉換成tf能夠識別的格式
image=tf.cast(image,tf.string)
label=tf.cast(label,tf.int32)
#生成佇列
input_queue=tf.train.slice_input_producer([image,label])
image_contents=tf.read_file(input_queue[0])
label=input_queue[1]
image=tf.image.decode_jpeg(image_contents,channels=3)#
# 統一圖片大小read_file()讀取圖片之後,要按照圖片格式進行解碼。本例程中訓練資料是jpg格式的,所以使用decode_jpeg()解碼器,如果是其他格式,就要用其他解碼器,具
# 視訊方法
# image = tf.image.resize_image_with_crop_or_pad(image, image_W, image_H)
# 我的方法
image = tf.image.resize_images(image, [image_H, image_W], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
image = tf.cast(image, tf.float32)
# image = tf.image.per_image_standardization(image) # 標準化資料
image_batch, label_batch = tf.train.batch([image, label],
batch_size=batch_size,
num_threads=64, # 執行緒
capacity=capacity)
# 這行多餘?
# label_batch = tf.reshape(label_batch, [batch_size])
return image_batch, label_batch
#定義損失函式,訓練函式,評估函式
def losses(logits, labels):
with tf.variable_scope("loss") as scope:
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,
labels=labels, name="xentropy_per_example")
loss = tf.reduce_mean(cross_entropy, name="loss")
tf.summary.scalar(scope.name + "loss", loss)
return loss
#是將稀疏表示的label與輸出層計算出來結果做對比,然後因為訓練的時候是16張圖片一個batch,
#所以再用tf.reduce_mean求一下平均值,就得到了這個batch的平均loss
def trainning(loss, learning_rate):
with tf.name_scope("optimizer"):
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
global_step = tf.Variable(0, name="global_step", trainable=False)
train_op = optimizer.minimize(loss, global_step=global_step)
return train_op
def evaluation(logits, labels):
with tf.variable_scope("accuracy") as scope:
correct = tf.nn.in_top_k(logits, labels, 1)
correct = tf.cast(correct, tf.float16)
accuracy = tf.reduce_mean(correct)
tf.summary.scalar(scope.name + "accuracy", accuracy)
return accuracy
3開始訓練
import os
import numpy as np
import tensorflow as tf
# import input_data
# import model
N_CLASSES = 2
IMG_H = 208
IMG_W = 208
BATCH_SIZE = 32
CAPACITY = 2000
MAX_STEP = 15000
learning_rate = 0.0001
train_dir = "data/train/"
logs_train_dir = "log/"
train, train_label = get_files(train_dir)
train_batch, train_label_batch = get_batch(train,
train_label,
IMG_W,
IMG_H,
BATCH_SIZE,
CAPACITY)
train_logits = inference(train_batch, BATCH_SIZE, N_CLASSES)
train_loss = losses(train_logits, train_label_batch)
train_op = trainning(train_loss, learning_rate)
train_acc = evaluation(train_logits, train_label_batch)
summary_op = tf.summary.merge_all()
sess = tf.Session()
train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
saver = tf.train.Saver()
sess.run(tf.global_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
try:
for step in np.arange(MAX_STEP):
if coord.should_stop():
break
_, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc])
if step % 100 == 0:
print("Step %d, train loss = %.2f, train accuracy = %.2f%%" % (step, tra_loss, tra_acc))
summary_str = sess.run(summary_op)
train_writer.add_summary(summary_str, step)
if step % 2000 == 0 or (step + 1) == MAX_STEP:
checkpoint_path = os.path.join(logs_train_dir, "model.ckpt")
saver.save(sess, checkpoint_path, global_step=step)
except tf.errors.OutOfRangeError:
print("Done training -- epoch limit reached.")
finally:
coord.request_stop()
coord.join(threads)
sess.close()
4評估模型,載入圖片,進行測試
import os
import numpy as np
import tensorflow as tf
# import input_data
# import model
N_CLASSES = 2
IMG_H = 208
IMG_W = 208
BATCH_SIZE = 32
CAPACITY = 2000
MAX_STEP = 15000
learning_rate = 0.0001
train_dir = "data/train/"
logs_train_dir = "log/"
train, train_label = get_files(train_dir)
train_batch, train_label_batch = get_batch(train,
train_label,
IMG_W,
IMG_H,
BATCH_SIZE,
CAPACITY)
train_logits = inference(train_batch, BATCH_SIZE, N_CLASSES)
train_loss = losses(train_logits, train_label_batch)
train_op = trainning(train_loss, learning_rate)
train_acc = evaluation(train_logits, train_label_batch)
summary_op = tf.summary.merge_all()
sess = tf.Session()
train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
saver = tf.train.Saver()
sess.run(tf.global_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
try:
for step in np.arange(MAX_STEP):
if coord.should_stop():
break
_, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc])
if step % 100 == 0:
print("Step %d, train loss = %.2f, train accuracy = %.2f%%" % (step, tra_loss, tra_acc))
summary_str = sess.run(summary_op)
train_writer.add_summary(summary_str, step)
if step % 2000 == 0 or (step + 1) == MAX_STEP:
checkpoint_path = os.path.join(logs_train_dir, "model.ckpt")
saver.save(sess, checkpoint_path, global_step=step)
except tf.errors.OutOfRangeError:
print("Done training -- epoch limit reached.")
finally:
coord.request_stop()
coord.join(threads)
sess.close()
執行結果是一張圖片:
載入模型進行測試
train_dir = "data/train/"
train, train_label = get_files(train_dir)
image_array = get_one_image(train)
with tf.Graph().as_default():
BATCH_SIZE = 1
N_CLASSES = 2
image = tf.cast(image_array, tf.float32)
image = tf.reshape(image, [1, 208, 208, 3])
logit = inference(image, BATCH_SIZE, N_CLASSES)
logit = tf.nn.softmax(logit)
x = tf.placeholder(tf.float32, shape=[208, 208, 3])
logs_train_dir = "log/"
saver = tf.train.Saver()
with tf.Session() as sess:
print("Reading checkpoints...")
ckpt = tf.train.get_checkpoint_state(logs_train_dir)
if ckpt and ckpt.model_checkpoint_path:
global_step = ckpt.model_checkpoint_path.split("/")[-1].split("-")[-1]
saver.restore(sess, ckpt.model_checkpoint_path)
print("Loading success, global_step is %s" % global_step)
else:
print("No checkpoint file found")
prediction = sess.run(logit, feed_dict={x: image_array})
max_index = np.argmax(prediction)
if max_index == 0:
print("This is a cat with possibility %.6f" % prediction[:, 0])
else:
print("This is a dog with possibility %.6f" % prediction[:, 1])
參考文獻
基於TensorFlow的Cats vs. Dogs(貓狗大戰)實現和詳解(2) - Sual - CSDN部落格 https://blog.csdn.net/qq_16137569/article/details/72830964
基於TensorFlow的Cats vs. Dogs(貓狗大戰)實現和詳解(1) - Sual - CSDN部落格 https://blog.csdn.net/qq_16137569/article/details/72802387
相關文章
- 機器學習是如何區分貓和狗的?機器學習
- 0802-程式設計實戰_貓和狗二分類_深度學習專案架構程式設計深度學習架構
- 通用mapper和分類實現APP
- 使用PagedDataSource類實現DataList和Repeater控制元件的分頁顯示 (轉)控制元件
- 一個分數類的實現——Rational類
- PHP無限級分類的實現(不使用遞迴)PHP遞迴
- Bert文字分類實踐(一):實現一個簡單的分類模型文字分類模型
- iOS 類知乎”分頁”效果的實現?iOS
- 如何基於TensorFlow使用LSTM和CNN實現時序分類任務CNN
- Pytorch實現分類器PyTorch
- 使用Harr特徵的級聯分類器實現目標檢測特徵
- 選單的無限極分類實現
- 使用sklearn實現svm--用於機械故障分類
- 軟體測試工具的分類和使用
- PHP實現無限極分類PHP
- 貝葉斯實現文字分類C++實現文字分類C++
- 英偉達新研究:“狗生貓,貓生萬物”的多模態無監督影像轉換
- 使用PaddleFluid和TensorFlow實現影象分類網路SE_ResNeXtUI
- 使用PaddleFluid和TensorFlow實現影像分類網路SE_ResNeXtUI
- 概率分類之樸素貝葉斯分類(垃圾郵件分類python實現)Python
- 破解垃圾分類難題,智慧分類如何實現最優解?
- 使用遞迴實現樹狀選單(無限級分類)遞迴
- 實現腦電訊號的情緒分類
- 分類 和 聚類聚類
- Laravel 框架實現無限極分類Laravel框架
- mysql 無限級分類實現思路MySql
- 基於Pytorch實現貓狗分類PyTorch
- jQuery實現左側分類選單jQuery
- Mahout分類演算法學習之實現Naive Bayes分類示例演算法AI
- PB下使用TreeView控制元件實現多級分類檢索View控制元件
- [kotlin]帶分類的RecyclerView通用實現新思路KotlinView
- PHP + MySQL 無限分類實現的2種方法PHPMySql
- CSDN部落格分類系統的分析與實現
- 14-宣告和實現的分離
- 教程 | 用Scikit-Learn實現多類別文字分類文字分類
- 【資料庫使用-oracle索引的建立和分類】二資料庫Oracle索引
- 【資料庫使用-oracle索引的建立和分類】一資料庫Oracle索引
- 樸素貝葉斯分類和預測演算法的原理及實現演算法