91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 9KB
    文件類型: .zip
    金幣: 2
    下載: 0 次
    發(fā)布日期: 2021-06-06
  • 語言: 其他
  • 標(biāo)簽: tensorflow??python??

資源簡介

采用tensorflow實(shí)現(xiàn)貓狗的識(shí)別。首先是進(jìn)行模型的撰寫,然后訓(xùn)練,最后測試。具體的訓(xùn)練圖片在網(wǎng)上下載即可

資源截圖

代碼片段和文件信息

#%%
import?tensorflow?as?tf
import?numpy?as?np
import?os

#?
img_width?=?208
img_height?=?208


#%%?獲取圖片?及?生成標(biāo)簽
train_dir?=?‘G:/tensorflow/cats_vs_dogs/data/train/‘

def?get_files(file_dir):
????‘‘‘
????args:
????????file_dir:?file?directory
????Returns:
????????ist?of?images?and?labels
????‘‘‘
????cats?=?[]
????label_cats?=?[]
????dogs?=?[]
????label_dogs?=?[]
????for?file?in?os.listdir(file_dir):
????????name?=?file.split(‘.‘)
????????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))?##?將圖像標(biāo)簽堆疊在一起
????
????temp?=?np.array([image_list?label_list])
????temp?=?temp.transpose()?#矩陣轉(zhuǎn)置
????np.random.shuffle(temp)?#?打亂存放的順序
????
????image_list?=?list(temp[:?0])?#?獲取圖片
????label_list?=?list(temp[:?1])?#?獲取標(biāo)簽
????label_list?=?[float(i)?for?i?in?label_list]
????
????return?image_list?label_list

#%%
????#?對(duì)圖片進(jìn)行裁剪
def?get_batch(image?label?image_W?image_H?batch_size?capacity):
????‘‘‘
????args:
????????image:?list?type
????????label:?list?type
????????image_W:?image_width
????????image_H:?image_Height
????????batch_size:batch?size?#每批次的圖像量
????????capacity:?the?maxmum?elements?in?queue
????Returns:
????????image_batch:?4D?tensor?[batch_size?width?height?3]dtype=tf.float32
????????label_batch:?1D?tensor?[batch_size]?dtype?=?tf.float32
????‘‘‘
????#?類型轉(zhuǎn)換函數(shù),返回張量
????image?=?tf.cast(image?tf.string)?#?數(shù)據(jù)類型轉(zhuǎn)換?image->string
????label?=?tf.cast(label?tf.int32)??#?數(shù)據(jù)類型轉(zhuǎn)換?label->int32
????
????#?make?an?input?queue?生成輸入對(duì)列
????input_queue?=?tf.train.slice_input_producer([image?label])
????
????label?=?input_queue[1]?#?讀取標(biāo)簽
????image_contents?=?tf.read_file(input_queue[0])?#?讀取圖像?string類型
????image?=?tf.image.decode_jpeg(image_contents?channels?=?3)?#解碼

????########################################
????#?data?argumentatioan?should?go?to?here
????########################################
????#?對(duì)圖片進(jìn)行裁剪或擴(kuò)充【在圖像中心處裁剪】,統(tǒng)一大小
????image?=?tf.image.resize_image_with_crop_or_pad(image?image_W?image_H)
????#?數(shù)據(jù)標(biāo)準(zhǔn)化?訓(xùn)練前需要對(duì)數(shù)據(jù)進(jìn)行標(biāo)準(zhǔn)化
????image?=?tf.image.per_image_standardization(image)?
????#?生成批次?在輸入的tensor中創(chuàng)建一些tensor數(shù)據(jù)的batch
????image_batch?label_batch?=?tf.train.batch([image?label]
??????????????????????????????????????????????batch_size?=?batch_size
??????????????????????????????????????????????num_threads?=?64
??????????????????????????????????????????????capacity?=?capacity)?
????#?重新生成大小,即將label_batch變換成[batch_size]行的形式
????label_batch?=?tf.reshape(label_batch?[batch_size])
????
????return?image_batch?label_batch
????
????#%%?test?:??matplotlib.pyplot繪圖?繪制直線、條形/矩形區(qū)域
????import?matplotlib.pyplot?as?plt

????BATCH_SIZE?=?5?#?批次中的圖像數(shù)量
????CAPACITY?

?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????4786??2017-09-13?21:11??import_data.py
?????文件????????6955??2017-09-13?21:34??model.py
?????文件???????11297??2017-08-31?10:14??test_import_data.py
?????文件????????6859??2017-09-14?09:54??training.py

評(píng)論

共有 條評(píng)論