-
大小: 84.53MB文件類型: .rar金幣: 2下載: 1 次發布日期: 2022-09-08
- 語言: 其他
- 標簽: tensorflow??Inception-v3??遷移學習??
資源簡介
tensorflow利用Inception-v3實現遷移學習。加載已有的模型,作為新任務提取特征的操作,實現分類遷移。包含完整的代碼和數據集
代碼片段和文件信息
#photo地址:
#http://download.tensorflow.org/example_images/flower_photos.tgz
#Inception-v3模型
#https://storage.googleapis.com/download.tensorflow.org/models/inception_dec_2015.zip
import?glob
import?os.path
import?random
import?numpy?as?np
import??tensorflow?as?tf
from?tensorflow.python.platform?import?gfile
#Inception-v3模型瓶頸層得節點個數
BOTTLENECK_TENSOR_SIZE=2048
BOTTLENECK_TENSOR_NAME=‘pool_3/_reshape:0‘
JPEG_DATA_TENSOR_NAME=‘DecodeJpeg/contents:0‘
MODEL_DIR=‘C:/Users/casgj/PycharmProjects/CNN/inception_dec_2015‘
MODEL_FILE=‘tensorflow_inception_graph.pb‘
CACHE_DIR=‘C:/Users/casgj/PycharmProjects/CNN/bottleneck‘
INPUT_DATA=‘C:/Users/casgj/PycharmProjects/CNN/flower_photos‘
#驗證得數據百分比
VALIDATION_PERCENTAGE=10
#測試得數據百分比
TEST_PERCENTAGE=10
#定義神經網絡得設置
LEARNING_RATE=0.01
STEPS=4000
BATCH=100
#從數據文件夾讀取所有得圖片列表并按訓練、驗證、測試數據分開
def?create_image_lists(testing_percentage?validation_percentage):
????result?=?{}
????sub_dirs?=?[x[0]?for?x?in?os.walk(INPUT_DATA)]
????is_root_dir?=?True
????for?sub_dir?in?sub_dirs:
????????if?is_root_dir:
????????????is_root_dir?=?False
????????????continue
????????extensions?=?[‘jpg‘?‘jpeg‘?‘JPG‘?‘JPEG‘]
????????file_list?=?[]
????????dir_name?=?os.path.basename(sub_dir)
????????for?extension?in?extensions:
????????????file_glob?=?os.path.join(INPUT_DATA?dir_name?‘*.‘?+?extension)
????????????file_list.extend(glob.glob(file_glob))
????????if?not?file_list:
????????????continue
????????label_name?=?dir_name.lower()
????????#?初始化
????????training_images?=?[]
????????testing_images?=?[]
????????validation_images?=?[]
????????for?file_name?in?file_list:
????????????base_name?=?os.path.basename(file_name)
????????????#?隨機劃分數據
????????????chance?=?np.random.randint(100)??#產生一個<100得正整數
????????????if?chance?????????????????validation_images.append(base_name)
????????????elif?chance?(testing_percentage?+?validation_percentage):
????????????????testing_images.append(base_name)
????????????else:
????????????????training_images.append(base_name)
????????result[label_name]?=?{
????????????‘dir‘:?dir_name
????????????‘training‘:?training_images
????????????‘testing‘:?testing_images
????????????‘validation‘:?validation_images
????????}
????return?result
#獲取給定類別category中index圖片的地址
def?get_image_path(image_listsimage_dirlabel_nameindexcategory):
????label_lists=image_lists[label_name]
????category_list=label_lists[category]
????mod_index=index%?len(category_list)
????base_name=category_list[mod_index]
????sub_dir=label_lists[‘dir‘]
????full_path=os.path.join(image_dirsub_dirbase_name)
????return?full_path
def?get_bottleneck_path(image_listslabel_nameindexcategory):
????return?get_image_path(image_listsCACHE_DIR
???????????label_nameindexcategory)+‘.txt‘
def?run_bottleneck_on_image(sessimage_dataimage_data_tensorbottleneck_tensor):
????bottleneck_values=sess.run(bottleneck_te
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???88631107??2018-10-01?12:03??inception_dec_2015.zip
?????文件??????10466??2018-10-02?15:54??遷移學習.py
-----------?---------??----------?-----??----
?????????????88641573????????????????????2
評論
共有 條評論