python - How to create a dataset of images with custom labels to train a pre-trained keras model -
i having problems making dataset using pre-trained keras inception-v3 model.
know there problem code.
traceback s follows:
...[info] completed label - human [info] completed label - human [info] completed label - human [info] completed label - human [info] completed label - human [status] training labels: [] [status] training labels shape: (0,) [status] saved model , weights disk.. [status] features , labels saved..
i using python 2.7 on ubuntu
from keras.applications.inception_v3 import inceptionv3, preprocess_input keras.preprocessing import image keras.models import model keras.models import model_from_json keras.layers.core import flatten sklearn.preprocessing import labelencoder import numpy np import glob import cv2 import h5py import os import json import cpickle import datetime open('conf/conf.json') f: config = json.load(f) model_name = config["model"] weights = config["weights"] include_top = config["include_top"] train_path = config["train_path"] features_path = config["features_path"] labels_path = config["labels_path"] test_size = config["test_size"] results = config["results"] model_path = config["model_path"] base_model = inceptionv3(weights=weights) model = model(input=base_model.input, output=base_model.output) image_size = (299, 299) print "[info] loaded base model , model..." train_labels = os.listdir(train_path) print("[info] encoding labels...") le = labelencoder() le.fit([tl tl in train_labels]) features = [] labels = [] i, label in enumerate(train_labels): cur_path = train_path + "/" + label label = "human" image_path in glob.glob(cur_path + "/*.jpg"): img = image.load_img(image_path, target_size=image_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) feature = model.predict(x)s flat = feature.flatten() features.append(flat) labels.append(label) print "[info] processed - {}".format(i) print "[info] completed label - {}".format(label) targetnames = np.unique(labels) le = labelencoder() le_labels = le.fit_transform(labels) print "[status] training labels: {}".format(le_labels) print "[status] training labels shape: {}".format(le_labels.shape) h5f_data = h5py.file(features_path, 'w') h5f_data.create_dataset('dataset_1', data=np.array(features)) h5f_label = h5py.file(labels_path, 'w') h5f_label.create_dataset('dataset_1', data=np.array(le_labels)) h5f_data.close() h5f_label.close() model_json = model.to_json() open(model_path + str(test_size) + ".json", "w") json_file: json_file.write(model_json) model.save_weights(model_path + str(test_size) + ".h5") print("[status] saved model , weights disk..") print "[status] features , labels saved.."
Comments
Post a Comment