# import the necessary packagesimport os# initialize the path to the *original* input directory of imagesORIG_INPUT_DATASET ="Food-5K"# initialize the base path to the *new* directory that will contain# our images after computing the training and testing splitBASE_PATH ="datasets/images/rps"# define the names of the training, testing, and validation# directoriesTRAIN ="train"TEST ="test"VAL ="val"# initialize the list of class label namesCLASSES = ["rock", "paper", "scissors"]# set the batch sizeBATCH_SIZE =32# initialize the label encoder file path and the output directory to# where the extracted features (in CSV file format) will be storedLE_PATH = os.path.sep.join(["rpsoutput2", "le.cpickle"])BASE_CSV_PATH ="rpsoutput2"
# import the necessary packagesfrom sklearn.preprocessing import LabelEncoderfrom tensorflow.keras.applications import ResNet50from tensorflow.keras.applications import VGG16from tensorflow.keras.applications.resnet50 import preprocess_inputfrom tensorflow.keras.preprocessing.image import img_to_arrayfrom tensorflow.keras.preprocessing.image import load_imgfrom imutils import pathsimport numpy as npimport pickleimport randomimport os# load the ResNet50 network and initialize the label encoderprint("[INFO] loading network...")model = VGG16(weights="imagenet", include_top=False)le =None# loop over the data splitsfor split in (TRAIN, TEST, VAL):# grab all image paths in the current splitprint("[INFO] processing '{} split'...".format(split)) p = os.path.sep.join([BASE_PATH, split]) imagePaths =list(paths.list_images(p))# randomly shuffle the image paths and then extract the class# labels from the file paths random.shuffle(imagePaths) labels = [p.split(os.path.sep)[-2] for p in imagePaths]# if the label encoder is None, create itif le isNone: le = LabelEncoder() le.fit(labels)# open the output CSV file for writing csvPath = os.path.sep.join([BASE_CSV_PATH,"{}.csv".format(split)]) csv =open(csvPath, "w")# loop over the images in batchesfor (b, i) inenumerate(range(0, len(imagePaths), BATCH_SIZE)):# extract the batch of images and labels, then initialize the# list of actual images that will be passed through the network# for feature extractionprint("[INFO] processing batch {}/{}".format(b +1,int(np.ceil(len(imagePaths) /float(BATCH_SIZE))))) batchPaths = imagePaths[i:i + BATCH_SIZE] batchLabels = le.transform(labels[i:i + BATCH_SIZE]) batchImages = []# loop over the images and labels in the current batchfor imagePath in batchPaths:# load the input image using the Keras helper utility# while ensuring the image is resized to 224x224 pixels image = load_img(imagePath, target_size=(224, 224)) image = img_to_array(image)# preprocess the image by (1) expanding the dimensions and# (2) subtracting the mean RGB pixel intensity from the# ImageNet dataset image = np.expand_dims(image, axis=0) image = preprocess_input(image)# add the image to the batch batchImages.append(image)# pass the images through the network and use the outputs as# our actual features, then reshape the features into a# flattened volume batchImages = np.vstack(batchImages) features = model.predict(batchImages, batch_size=BATCH_SIZE) features = features.reshape((features.shape[0], 7*7*512))# loop over the class labels and extracted featuresfor (label, vec) inzip(batchLabels, features):# construct a row that exists of the class label and# extracted features vec =",".join([str(v) for v in vec]) csv.write("{},{}\n".format(label, vec))# close the CSV file csv.close()# serialize the label encoder to diskf =open(LE_PATH, "wb")f.write(pickle.dumps(le))f.close()
2024-06-12 15:58:34.912300: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-12 15:58:34.924843: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-12 15:58:34.925545: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-12 15:58:34.927471: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-12 15:58:34.928240: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-12 15:58:34.928610: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-12 15:58:35.002875: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-12 15:58:35.003115: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-12 15:58:35.003314: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2024-06-12 15:58:35.003476: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1928] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 226 MB memory: -> device: 0, name: NVIDIA GeForce GTX 960M, pci bus id: 0000:01:00.0, compute capability: 5.0
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1718207915.851926 803 service.cc:145] XLA service 0x77f444104fc0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
I0000 00:00:1718207915.851960 803 service.cc:153] StreamExecutor device (0): NVIDIA GeForce GTX 960M, Compute Capability 5.0
2024-06-12 15:58:35.863848: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:268] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
2024-06-12 15:58:35.970248: I external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:465] Loaded cuDNN version 8906
2024-06-12 15:58:35.991171: W external/local_tsl/tsl/framework/bfc_allocator.cc:296] Allocator (GPU_0_bfc) ran out of memory trying to allocate 408.00MiB with freed_by_count=0. The caller indicates that this is not a failure, but this may mean that there could be performance gains if more memory were available.
2024-06-12 15:58:35.993817: W tensorflow/core/framework/op_kernel.cc:1839] OP_REQUIRES failed at xla_ops.cc:580 : UNKNOWN: Failed to determine best cudnn convolution algorithm for:
%cudnn-conv-bias-activation.39 = (f32[32,64,224,224]{3,2,1,0}, u8[0]{0}) custom-call(f32[32,3,224,224]{3,2,1,0} %transpose.30, f32[64,3,3,3]{3,2,1,0} %transpose.31, f32[64]{0} %arg2.3), window={size=3x3 pad=1_1x1_1}, dim_labels=bf01_oi01->bf01, custom_call_target="__cudnn$convBiasActivationForward", metadata={op_type="Conv2D" op_name="vgg16_1/block1_conv1_1/convolution" source_file="/usr/local/lib/python3.11/dist-packages/tensorflow/python/framework/ops.py" source_line=1177}, backend_config={"operation_queue_id":"0","wait_on_operation_queues":[],"cudnn_conv_backend_config":{"conv_result_scale":1,"activation_mode":"kRelu","side_input_scale":0,"leakyrelu_alpha":0}}
Original error: RESOURCE_EXHAUSTED: Out of memory while trying to allocate 427819008 bytes.
To ignore this failure and try to use a fallback algorithm (which may have suboptimal performance), use XLA_FLAGS=--xla_gpu_strict_conv_algorithm_picker=false. Please also file a bug for the root cause of failing autotuning.
2024-06-12 15:58:35.993865: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: UNKNOWN: Failed to determine best cudnn convolution algorithm for:
%cudnn-conv-bias-activation.39 = (f32[32,64,224,224]{3,2,1,0}, u8[0]{0}) custom-call(f32[32,3,224,224]{3,2,1,0} %transpose.30, f32[64,3,3,3]{3,2,1,0} %transpose.31, f32[64]{0} %arg2.3), window={size=3x3 pad=1_1x1_1}, dim_labels=bf01_oi01->bf01, custom_call_target="__cudnn$convBiasActivationForward", metadata={op_type="Conv2D" op_name="vgg16_1/block1_conv1_1/convolution" source_file="/usr/local/lib/python3.11/dist-packages/tensorflow/python/framework/ops.py" source_line=1177}, backend_config={"operation_queue_id":"0","wait_on_operation_queues":[],"cudnn_conv_backend_config":{"conv_result_scale":1,"activation_mode":"kRelu","side_input_scale":0,"leakyrelu_alpha":0}}
Original error: RESOURCE_EXHAUSTED: Out of memory while trying to allocate 427819008 bytes.
To ignore this failure and try to use a fallback algorithm (which may have suboptimal performance), use XLA_FLAGS=--xla_gpu_strict_conv_algorithm_picker=false. Please also file a bug for the root cause of failing autotuning.
[[{{node StatefulPartitionedCall}}]]
UnknownError: Graph execution error:
Detected at node StatefulPartitionedCall defined at (most recent call last):
File "/usr/lib/python3.11/runpy.py", line 198, in _run_module_as_main
File "/usr/lib/python3.11/runpy.py", line 88, in _run_code
File "/usr/local/lib/python3.11/dist-packages/ipykernel_launcher.py", line 18, in <module>
File "/usr/local/lib/python3.11/dist-packages/traitlets/config/application.py", line 1075, in launch_instance
File "/usr/local/lib/python3.11/dist-packages/ipykernel/kernelapp.py", line 739, in start
File "/usr/local/lib/python3.11/dist-packages/tornado/platform/asyncio.py", line 205, in start
File "/usr/lib/python3.11/asyncio/base_events.py", line 604, in run_forever
File "/usr/lib/python3.11/asyncio/base_events.py", line 1909, in _run_once
File "/usr/lib/python3.11/asyncio/events.py", line 80, in _run
File "/usr/local/lib/python3.11/dist-packages/ipykernel/kernelbase.py", line 545, in dispatch_queue
File "/usr/local/lib/python3.11/dist-packages/ipykernel/kernelbase.py", line 534, in process_one
File "/usr/local/lib/python3.11/dist-packages/ipykernel/kernelbase.py", line 437, in dispatch_shell
File "/usr/local/lib/python3.11/dist-packages/ipykernel/ipkernel.py", line 359, in execute_request
File "/usr/local/lib/python3.11/dist-packages/ipykernel/kernelbase.py", line 778, in execute_request
File "/usr/local/lib/python3.11/dist-packages/ipykernel/ipkernel.py", line 446, in do_execute
File "/usr/local/lib/python3.11/dist-packages/ipykernel/zmqshell.py", line 549, in run_cell
File "/usr/local/lib/python3.11/dist-packages/IPython/core/interactiveshell.py", line 3075, in run_cell
File "/usr/local/lib/python3.11/dist-packages/IPython/core/interactiveshell.py", line 3130, in _run_cell
File "/usr/local/lib/python3.11/dist-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner
File "/usr/local/lib/python3.11/dist-packages/IPython/core/interactiveshell.py", line 3334, in run_cell_async
File "/usr/local/lib/python3.11/dist-packages/IPython/core/interactiveshell.py", line 3517, in run_ast_nodes
File "/usr/local/lib/python3.11/dist-packages/IPython/core/interactiveshell.py", line 3577, in run_code
File "/tmp/ipykernel_689/3128301997.py", line 63, in <module>
File "/usr/local/lib/python3.11/dist-packages/keras/src/utils/traceback_utils.py", line 118, in error_handler
File "/usr/local/lib/python3.11/dist-packages/keras/src/backend/tensorflow/trainer.py", line 513, in predict
File "/usr/local/lib/python3.11/dist-packages/keras/src/backend/tensorflow/trainer.py", line 212, in one_step_on_data_distributed
Failed to determine best cudnn convolution algorithm for:
%cudnn-conv-bias-activation.39 = (f32[32,64,224,224]{3,2,1,0}, u8[0]{0}) custom-call(f32[32,3,224,224]{3,2,1,0} %transpose.30, f32[64,3,3,3]{3,2,1,0} %transpose.31, f32[64]{0} %arg2.3), window={size=3x3 pad=1_1x1_1}, dim_labels=bf01_oi01->bf01, custom_call_target="__cudnn$convBiasActivationForward", metadata={op_type="Conv2D" op_name="vgg16_1/block1_conv1_1/convolution" source_file="/usr/local/lib/python3.11/dist-packages/tensorflow/python/framework/ops.py" source_line=1177}, backend_config={"operation_queue_id":"0","wait_on_operation_queues":[],"cudnn_conv_backend_config":{"conv_result_scale":1,"activation_mode":"kRelu","side_input_scale":0,"leakyrelu_alpha":0}}
Original error: RESOURCE_EXHAUSTED: Out of memory while trying to allocate 427819008 bytes.
To ignore this failure and try to use a fallback algorithm (which may have suboptimal performance), use XLA_FLAGS=--xla_gpu_strict_conv_algorithm_picker=false. Please also file a bug for the root cause of failing autotuning.
[[{{node StatefulPartitionedCall}}]] [Op:__inference_one_step_on_data_distributed_502]
# import the necessary packagesfrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Densefrom tensorflow.keras import Inputfrom tensorflow.keras.optimizers import SGDfrom tensorflow.keras.utils import to_categoricalfrom sklearn.metrics import classification_reportimport numpy as npimport pickleimport osdef csv_feature_generator(inputPath, bs, numClasses, mode="train"):# open the input file for reading f =open(inputPath, "r")# loop indefinitelywhileTrue:# initialize our batch of data and labels data = [] labels = []# keep looping until we reach our batch sizewhilelen(data) < bs:# attempt to read the next row of the CSV file row = f.readline()# check to see if the row is empty, indicating we have# reached the end of the fileif row =="":# reset the file pointer to the beginning of the file# and re-read the row f.seek(0) row = f.readline()# if we are evaluating we should now break from our# loop to ensure we don't continue to fill up the# batch from samples at the beginning of the fileif mode =="eval":break# extract the class label and features from the row row = row.strip().split(",") label = row[0] label = to_categorical(label, num_classes=numClasses) features = np.array(row[1:], dtype="float")# update the data and label lists data.append(features) labels.append(label)# yield the batch to the calling functionprint(np.array(data).shape)yield (np.array(data), np.array(labels))# load the label encoder from diskle = pickle.loads(open(LE_PATH, "rb").read())# derive the paths to the training, validation, and testing CSV filestrainPath = os.path.sep.join([BASE_CSV_PATH,"{}.csv".format(TRAIN)])valPath = os.path.sep.join([BASE_CSV_PATH,"{}.csv".format(VAL)])testPath = os.path.sep.join([BASE_CSV_PATH,"{}.csv".format(TEST)])# determine the total number of images in the training and validation# setstotalTrain =sum([1for l inopen(trainPath)])totalVal =sum([1for l inopen(valPath)])# extract the testing labels from the CSV file and then determine the# number of testing imagestestLabels = [int(row.split(",")[0]) for row inopen(testPath)]totalTest =len(testLabels)# construct the training, validation, and testing generatorstrainGen = csv_feature_generator(trainPath, BATCH_SIZE,len(CLASSES), mode="train")valGen = csv_feature_generator(valPath, BATCH_SIZE,len(CLASSES), mode="eval")testGen = csv_feature_generator(testPath, BATCH_SIZE,len(CLASSES), mode="eval")
ValueError: invalid literal for int() with base 10: ''
# make predictions on the testing images, finding the index of the# label with the corresponding largest predicted probability, then# show a nicely formatted classification reportprint("[INFO] evaluating network...")predIdxs = model.predict(x=testGen, steps=(totalTest //BATCH_SIZE) +1)predIdxs = np.argmax(predIdxs, axis=1)print(classification_report(testLabels, predIdxs, target_names=le.classes_))