【關於文件夾】
這里Keras是在Windows環境,使用Anaconda安裝
Anaconda有兩個主要文件夾需要了解:
1 Anaconda 應用程序安裝目錄下的Keras子文件夾,需要搜索找到
2 Anaconda 應用程序存儲Keras模型和數據集文件的文件在 ,用對應的用戶文件夾下的.kears文件夾下,注意有個.,實在找不見可以搜索
【數據集】:下載后默認存儲目錄 C:\Users\Administrator\.keras\datasets下的同名文件,注意有個點
執行下載時,要import相應的模塊,利用數據集模塊提供的函數下載數據,模塊文件結構如下圖所示: 在Anaconda安裝文件夾下搜索keras即可找到此目錄
以 cifar10數據集模塊 為例,內容如下所示:
1 # Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ============================================================================== 15 """CIFAR10 small images classification dataset. 16 """ 17 from __future__ import absolute_import 18 from __future__ import division 19 from __future__ import print_function 20 21 import os 22 23 import numpy as np 24 25 from tensorflow.python.keras import backend as K 26 from tensorflow.python.keras.datasets.cifar import load_batch 27 from tensorflow.python.keras.utils.data_utils import get_file 28 from tensorflow.python.util.tf_export import tf_export 29 30 31 @tf_export('keras.datasets.cifar10.load_data') 32 def load_data(): 33 """Loads CIFAR10 dataset. 34 35 Returns: 36 Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`. 37 """ 38 dirname = 'cifar-10-batches-py' 39 origin = 'https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz' 40 path = get_file(dirname, origin=origin, untar=True) 41 42 num_train_samples = 50000 43 44 x_train = np.empty((num_train_samples, 3, 32, 32), dtype='uint8') 45 y_train = np.empty((num_train_samples,), dtype='uint8') 46 47 for i in range(1, 6): 48 fpath = os.path.join(path, 'data_batch_' + str(i)) 49 (x_train[(i - 1) * 10000:i * 10000, :, :, :], 50 y_train[(i - 1) * 10000:i * 10000]) = load_batch(fpath) 51 52 fpath = os.path.join(path, 'test_batch') 53 x_test, y_test = load_batch(fpath) 54 55 y_train = np.reshape(y_train, (len(y_train), 1)) 56 y_test = np.reshape(y_test, (len(y_test), 1)) 57 58 if K.image_data_format() == 'channels_last': 59 x_train = x_train.transpose(0, 2, 3, 1) 60 x_test = x_test.transpose(0, 2, 3, 1) 61 62 return (x_train, y_train), (x_test, y_test)
dirname = 'cifar-10-batches-py' #指明下載后的文件的存儲目錄 origin = 'https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz' #指明要從哪個網址下載
在程序中需要下載對應的數據集時,首先導入對應的模塊,然后調用.load_data()函數
1 #從Keras導入相應的模塊 2 from keras.datasets import cifar10 #這里的cifar10 對應上面的cifar10.py 3 4 #下載數據集,這里使用cifar10數據集 5 (x_train, y_train), (x_validate, y_validate) = cifar10.load_data() #下載的好慢,要一個半小時!
下載完成后,保存數據的文件夾內如下圖所示

當在程序中執行下載較慢時,也可從GitHub上下載下來后,替換對應的文件
【模型】:下載后默認存儲目錄 C:\Users\Administrator\.keras\datasets下的同名文件,注意有個點
Keras中的模型文件封裝在 Application 模塊中,詳細請查看官方中文文檔,文檔寫的很全面:https://keras-cn.readthedocs.io/en/latest/
模型相關文件位於與dataset同級的applications文件加下--注意,是Anaconda應用程序安裝目錄文件夾的子文件夾
Keras 提供的模型如下圖所示:
在 程序中實例化相應的模型時,會自動下載對應的模型文件並存儲到 C:\Users\Administrator\.keras\datasets 目錄中。
1 base_model = InceptionV3(weights='imagenet',include_top=False) #加載模型,不包含頂層
在程序內下載模型較慢時(例如 Inception v3模型大小有80多兆,我這下載顯示要3個小時),可以下載下來后,替換對應的文件。
下載地址可以在程序執行失敗時根據錯誤信息獲得。也可在applications文件夾下對應的文件內找到.
下面時Inception v3.py 文件中關於下載地址的部分
1 WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels.h5' 2 WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'