【问题标题】:Unable to download CIFAR-10 dataset无法下载 CIFAR-10 数据集
【发布时间】:2022-12-04 08:57:05
【问题描述】:

我正在尝试运行 GoogLeNet 代码,但是当我运行它时,出于某种原因它说

[INFO] loading CIFAR-10 data...
[INFO] compiling model..

但是当我的朋友运行相同的代码时,他的节目

[INFO] loading CIFAR-10 data...
Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
[INFO] compiling model..

有没有我的不显示的原因?我已经在下面发布了完整的代码,这与我的朋友也在运行的代码完全相同。我们系统的唯一区别是他的笔记本电脑没有 GPU,而我的台式机在 GEFORCE RTX 3080 上运行。

# Python: 3.6
# keras: 2.2.4 for GoogLeNet on CIFAR-10
# Tensorflow :1.13.0
# cuda toolkit: 10.0
# cuDNN: 7.4.2
# scikit-learn 0.20.2
# Imutils
# NumPy

# set the matplotlib backend so figures can be saved in the background
import matplotlib
matplotlib.use("Agg")

# import packages
from sklearn.metrics import classification_report
from sklearn.preprocessing import LabelBinarizer
from pipeline.nn.conv import MiniGoogLeNet
from pipeline.callbacks import TrainingMonitor
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import LearningRateScheduler
from keras.optimizers import SGD
from keras.datasets import cifar10
import numpy as np
import argparse
import os

# define the total number of epochs to train for along with initial learning rate
NUM_EPOCHS =70
INIT_LR = 5e-3

def poly_decay(epoch):
    # initialize the maximum number of epochs, base learning rate,
    # and power of the polynomial
    maxEpochs = NUM_EPOCHS
    baseLR = INIT_LR
    power = 1.0

    # compute the new learning rate based on polynomial decay
    alpha = baseLR * (1 - (epoch / float(maxEpochs))) ** power

    # return the new learning rate
    return alpha

# construct the argument parser
ap = argparse.ArgumentParser()
ap.add_argument("-m", "--model", required = True, help = "path to output model")
ap.add_argument("-o", "--output", required = True,
    help = "path to output directory (logs, plots, etc.)")
args = vars(ap.parse_args())

# load the training and testing data, converting the image from integers to floats
print("[INFO] loading CIFAR-10 data...")
((trainX, trainY), (testX, testY)) = cifar10.load_data()
trainX = trainX.astype("float")
testX = testX.astype("float")
# apply mean subtraction to the data
mean = np.mean(trainX, axis = 0)
trainX -= mean
testX -= mean

# convert the labels from integers to vectors
lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)

# initialize the label name for CIFAR-10 dataset
labelNames = ["airplane", "automobile", "bird", "cat", "deer",
    "dog", "frog", "horse", "ship", "truck"]

# construct the image generator for data augmentation
aug = ImageDataGenerator(width_shift_range = 0.1, height_shift_range = 0.1,
    horizontal_flip = True, fill_mode = "nearest")

# construct the set of callbacks
figPath = os.path.sep.join([args["output"], "{}.png".format(os.getpid())])
jsonPath = os.path.sep.join([args["output"], "{}.json".format(os.getpid())])
callbacks = [TrainingMonitor(figPath, jsonPath = jsonPath),
    LearningRateScheduler(poly_decay)]

# initialize the optimizer and model
print("[INFO] compiling model...")
opt = SGD(lr = INIT_LR, momentum = 0.9)
model = MiniGoogLeNet.build(width = 32, height = 32, depth = 3, classes = 10)
model.compile(loss = "categorical_crossentropy", optimizer = opt, metrics = ["accuracy"])
#model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=0.001), loss = 'categorical_crossentropy', metrics = ["accuracy"])

# train the network
print("[INFO] training network...")
model.fit(aug.flow(trainX, trainY, batch_size = 64),
    validation_data = (testX, testY), steps_per_epoch = len(trainX) // 64,
    epochs = NUM_EPOCHS, callbacks = callbacks, verbose = 1)

# evaluate network
print("[INFO] evaluating network...")
predictions = model.predict(testX, batch_size = 64)
print(classification_report(testY.argmax(axis = 1),
    predictions.argmax(axis = 1), target_names = labelNames))

# save the network to disk
print("[INFO] serializing network...")
model.save(args["model"])

#Run Command: python.exe googlenet_cifar10.py --model output/minigooglenet_cifar10.hdf5 --output output

【问题讨论】:

    标签: python tensorflow conv-neural-network


    【解决方案1】:

    你看过本地数据集吗?

    [本地数据集]:

    C:UsersJirayu Kaewprateep.kerasdatasets
    

    [ 样本 ]:

    import os
    from os.path import exists
    
    import tensorflow as tf
    import tensorflow_datasets as tfds
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
    None
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    physical_devices = tf.config.experimental.list_physical_devices('GPU')
    assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
    config = tf.config.experimental.set_memory_growth(physical_devices[0], True)
    print(physical_devices)
    print(config)
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Variables
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    class_10_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
    
    checkpoint_path = "F:\models\checkpoint\" + os.path.basename(__file__).split('.')[0] + "\TF_DataSets_01.h5"
    checkpoint_dir = os.path.dirname(checkpoint_path)
    
    if not exists(checkpoint_dir) : 
        os.mkdir(checkpoint_dir)
        print("Create directory: " + checkpoint_dir)
        
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    DataSet
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    (train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.cifar10.load_data()
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Model Initialize
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    model = tf.keras.models.Sequential([
        tf.keras.layers.InputLayer(input_shape=( 32, 32, 3 )),
        tf.keras.layers.Normalization(mean=3., variance=2.),
        tf.keras.layers.Normalization(mean=4., variance=6.),
        tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
        tf.keras.layers.MaxPooling2D((2, 2)),
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Reshape((128, 225)),
        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96, return_sequences=True, return_state=False)),
        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96)),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(192, activation='relu'),
        tf.keras.layers.Dense(10),
    ])
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Callback
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    class custom_callback(tf.keras.callbacks.Callback):
        def on_epoch_end(self, epoch, logs={}):
            if( logs['accuracy'] >= 0.97 ):
                self.model.stop_training = True
        
    custom_callback = custom_callback()
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Optimizer
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    optimizer = tf.keras.optimizers.Nadam(
        learning_rate=0.00001, beta_1=0.9, beta_2=0.999, epsilon=1e-07,
        name='Nadam'
    )
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Loss Fn
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""                               
    lossfn = tf.keras.losses.SparseCategoricalCrossentropy(
        from_logits=False,
        reduction=tf.keras.losses.Reduction.AUTO,
        name='sparse_categorical_crossentropy'
    )
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Model Summary
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    model.compile(optimizer=optimizer, loss=lossfn, metrics=['accuracy'])
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : FileWriter
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    if exists(checkpoint_path) :
        model.load_weights(checkpoint_path)
        print("model load: " + checkpoint_path)
        input("Press Any Key!")
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Training
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    history = model.fit( train_images, train_labels, batch_size=100, epochs=50, callbacks=[custom_callback] )
    model.save_weights(checkpoint_path)
    
    plt.figure(figsize=(6, 6))
    plt.title("Actors recognitions")
    for i in range( 36 ):
        img = tf.keras.preprocessing.image.array_to_img(
            test_images[i],
            data_format=None,
            scale=True
        )
        img_array = tf.keras.preprocessing.image.img_to_array(img)
        img_array = tf.expand_dims(img_array, 0)
        predictions = model.predict(img_array)
        score = tf.nn.softmax(predictions[0])
        plt.subplot(6, 6, i + 1)
        plt.xticks([])
        plt.yticks([])
        plt.grid(False)
        plt.imshow(test_images[i])
        plt.xlabel(str(round(score[tf.math.argmax(score).numpy()].numpy(), 2)) + ":" +  str(class_10_names[tf.math.argmax(score)]))
        
    plt.show()
    
    input('...')
    

    [ 输出 ]:

    【讨论】:

    • 你指的是这个本地数据集:C:UsersJoshG.kerasdatasetscifar-10-batches-py
    • 你能编辑我的代码以进行正确的更改吗@Jirayu Kaewprateep
    • 改正默认值,结果是什么⁉️
    猜你喜欢
    • 2017-12-20
    • 2017-12-19
    • 2018-06-22
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2019-08-23
    • 2017-05-15
    相关资源
    最近更新 更多