tenorflow. python .framework.errors_impl.NotFoundError: Не удалось создать каталог: ./models/model\variables; Данный файл или каталог отсутствует? - PullRequest
0 голосов
/ 12 апреля 2020
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import SGD, Adam
from tensorflow.keras.preprocessing.image import img_to_array
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from my_model.smallervggnet import SmallerVGGNet
import matplotlib.pyplot as plt
from imutils import paths
import numpy as np
import argparse
import random
import pickle
import cv2
import os
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau, TensorBoard
import matplotlib

matplotlib.use("Agg")

ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True,
                help="path to input dataset (i.e., directory of images)")
ap.add_argument("-m", "--model", required=True,
                help="path to output model")
ap.add_argument("-l", "--labelbin", required=True,
                help="path to output label binarizer")
ap.add_argument("-p", "--plot", type=str, default="plot.png",
                help="path to output accuracy/loss plot")
args = vars(ap.parse_args())

EPOCHS = 1
INIT_LR = 1e-3
BS = 10
IMAGE_HEIGHT = 180
IMAGE_WIDTH = 140

# Shuffling the images
print("[INFO] loading images...")
imagePaths = sorted(list(paths.list_images(args["dataset"])))
random.seed(42)
random.shuffle(imagePaths)

data = []
labels = []

# loop over the input images
for imagePath in imagePaths:
    image = cv2.imread(imagePath)
    gImg = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gImg = cv2.resize(gImg, (140, 180))
    image = cv2.resize(image, (IMAGE_HEIGHT, IMAGE_WIDTH))
    image = img_to_array(image)
    image = np.reshape(gImg, (IMAGE_HEIGHT, IMAGE_WIDTH, 1))

    data.append(image)

    # update the label list
    l = label = imagePath.split(os.path.sep)[-2].split("_")
    labels.append(l)

# scale the raw pixel intensities to the range [0, 1]
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)
print("[INFO] data matrix: {} images ({:.2f}MB)".format(
    len(imagePaths), data.nbytes / (1024 * 1000.0)))

# binarize the labels
print("[INFO] class labels:")
mlb = LabelBinarizer()
labels = mlb.fit_transform(labels)

# loop over each of the possible class labels and show them
for (i, label) in enumerate(mlb.classes_):
    print("{}. {}".format(i + 1, label))

(trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.2, random_state=42)

aug = ImageDataGenerator(rotation_range=25, width_shift_range=0.1,
                         height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,
                         horizontal_flip=True, fill_mode="nearest")

print("[INFO] compiling model...")
model = SmallerVGGNet.build(
    width=IMAGE_WIDTH, height=IMAGE_HEIGHT,
    depth=1, classes=len(mlb.classes_),
    finalact="sigmoid")

# initialize the optimizer (SGD is sufficient)
opt = SGD(lr=INIT_LR, decay=INIT_LR / EPOCHS)

model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])

# saving best weights checkpoints
target_dir_weight = './weights-checkpoints/'
if not os.path.exists(target_dir_weight):
    os.mkdir(target_dir_weight)
weight_name = target_dir_weight + 'model_weights.h5'
checkpoint = ModelCheckpoint(weight_name, monitor='val_loss', verbose=1, save_best_only=True, mode='min')

# early stopping
early = EarlyStopping(monitor='val_loss', patience=8, verbose=1, mode='min')
# best learning rate
lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5, verbose=0, mode='auto', cooldown=0, min_lr=0.0000001)

callbacks_list = [checkpoint, early, lr]

# train the network
print("[INFO] training network...")
H = model.fit_generator(
    aug.flow(trainX, trainY, batch_size=BS),
    validation_data=(testX, testY),
    steps_per_epoch=len(trainX)//BS,
    epochs=EPOCHS, verbose=1, callbacks=callbacks_list)

# save the model to disk
print("[INFO] serializing network...")
target_dir = './models/'
if not os.path.exists(target_dir):
    os.mkdir(target_dir)
model.save(target_dir + args["model"])

# save the multi-label binarizer to disk
print("[INFO] serializing label binarizer...")
target_dir_label = './label/'
if not os.path.exists(target_dir_label):
    os.mkdir(target_dir_label)
f = open(target_dir_label + args["labelbin"], "wb")
f.write(pickle.dumps(mlb))
f.close()

# plot the training loss and accuracy
plt.style.use("ggplot")
plt.figure()

plt.plot(H.history["loss"], label="train_loss")
plt.plot(H.history["val_loss"], label="val_loss")
plt.plot(H.history["acc"], label="train_acc")
plt.plot(H.history["val_acc"], label="val_acc")
plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="upper left")
target_dir_plot = './plots/'
if not os.path.exists(target_dir_plot):
    os.mkdir(target_dir_plot)
plt.savefig(target_dir_plot + args["plot"])

При использовании Keras передайте * _constraint аргументы слоям. Traceback (последний вызов был последним): файл "train.py", строка 120, в файле model.save (target_dir + args ["model"]) файл "C: \ Users \ rome sh .p \ AppData \ Локальный \ Continuum \ anaconda3 \ envs \ Neural Networks 2020 \ lib \ site-packages \ tenorflow_core \ python \ keras \ engine \ network.py ", строка 975, в сигнатурах сохранения, параметры) Файл" C: \ Users \ rome " sh .p \ AppData \ Local \ Continuum \ anaconda3 \ envs \ Neural Networks 2020 \ lib \ site-packages \ensor_flow_core \ python \ keras \ save \ save.py ", строка 115, в сигнатурах save_model, параметры) Файл" C: \ Users \ rome sh .p \ AppData \ Local \ Continuum \ anaconda3 \ envs \ Neural Networks 2020 \ lib \ site-packages \ tenorsflow_core \ python \ keras \ сохранение \ сохраненная_модель \ save.py ", строка 74, в save save_lib.save (модель, путь к файлу, подписи, параметры) Файл "C: \ Users \ rome sh .p \ AppData \ Local \ Continuum \ anaconda3 \ envs \ Neural Networks 2020 \ lib \ site- packages \ tenorsflow_core \ python \ save_model \ save.py ", строка 899, в файле save utils_impl.get_or_create_variables_dir (export_dir)" C: \ Users \ rome sh .p \ AppData \ Local \ Continuum \ anaconda3 \ envs \ Neural Networks 2020 \ lib \ site-packages \ensorflow_core \ python \ved_model \ utils_impl.py ", строка 183, в файле get_or_create_variables_dir file_io.recursive_create_dir (variable_ 1013) \ *: Пользователи \ rome sh .p \ AppData \ Local \ Continuum \ anaconda3 \ envs \ Нейронные сети 2020 \ lib \ site-packages \ensororflow_core \ python \ lib \ io \ file_io.py ", строка 438, в recursive_create_dir recursive_create_dir_v2 (dirname ) Файл "C: \ Users \ rome sh .p \ AppData \ Local \ Continuum \ anaconda3 \ envs \ Neural Networks 2020 \ lib \ site-packages \ tenorsflow_core \ python \ lib \ io \ file_io.py", строка 453, в recursive_create_dir_v2 pywrap_tensorflow.RecursivelyCreateDir (compat.as_bytes (path)) тензор потока. python .framework.errors_impl.NotFoundError: не удалось создать каталог: ./models/model\variables; Нет такого файла или каталога

...