Python 运行Keras Tensorflow脚本时如何处理GPU和CPU中的内存问题

Python 运行Keras Tensorflow脚本时如何处理GPU和CPU中的内存问题,python,numpy,tensorflow,memory,gpu,Python,Numpy,Tensorflow,Memory,Gpu,我正在运行一个脚本,该脚本基本上使用RESNET50 CNN以微调方式处理39937张224x224x3形状的图像。代码如下: import os import cv2 import numpy as np import sys import numpy as np from sklearn.metrics import classification_report, confusion_matrix from sklearn.model_selection import GridSearchC

我正在运行一个脚本,该脚本基本上使用RESNET50 CNN以微调方式处理39937张224x224x3形状的图像。代码如下:

import os
import cv2
import numpy as np
import sys
import numpy as np
from sklearn.metrics import classification_report, confusion_matrix 
from sklearn.model_selection import GridSearchCV 
from statistics import mode
from sklearn.metrics import roc_auc_score, average_precision_score, accuracy_score, f1_score, precision_score, recall_score
import pickle
from sklearn.metrics import plot_confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.utils import to_categorical
import tensorflow as tf
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model     
      
os.environ["CUDA_VISIBLE_DEVICES"]="0"

""" finetune_test_resnet()
   Finetune and test a Resnet50 CNN on 224x224 scanned documents data
       
    The code does the procedure below:
    1- Open the features for training, together with their labels
    2- Open Resnet50 labels from ImageNet
    2- Fine tune a Resnet50 CNN on the new data 
    3- Open the text file for testing
    4- Read and classify blocks for each document, the final label is the majority voting of all labels classified for blocks in that document

    usage: python3 resnet50.py file_train_classes, file_train_features test_filenames output_file
    where:
            file_train_classes= a .npy file containing training classes saved before in order to make experiments run faster
            file_train_features= another .npy file for training features
            test_pathnames= a .csv file containing the files for testing, where majority voting will be done for their blocks
            output_file=where resulting predicted labels will be saved for future confusion matrix calculations
"""

def finetune_test_resnet(file_train_classes, file_train_features, test_pathnames, output_file):
    #Lets load features and classes first
    print("Loading, organizing and pre-processing features")
    num_classes = 8
    x_train=np.load(file_train_features)
    y_train=np.load(file_train_classes)
    #Defining train as 70% and validation 30% of the data  
    #The partition is stratified with a fixed random state
    #Therefore, for all networks, the partition will be the same
    x_train, x_validation, y_train, y_validation = train_test_split(x_train, y_train, test_size=0.30, stratify=y_train, random_state=42)

    y_train = to_categorical(y_train, num_classes)
    y_validation = to_categorical(y_validation, num_classes)

    y_train= tf.constant(y_train, shape=[y_train.shape[0], num_classes])
    y_validation= tf.constant(y_validation, shape=[y_validation.shape[0], num_classes])
  
    #Preprocessing data
    x_train = x_train.astype('float32')
    x_validation=x_validation.astype('float32')
    x_train /= 255.
    x_validation /= 255.

    print("Setting up the network")
    #Parameters for network training
    batch_size = 8
    epochs=300
    #Use ADAM as it is the most used
    adam=tf.keras.optimizers.Adam(lr=0.01, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
    trainAug = ImageDataGenerator(rotation_range=30,zoom_range=0.15,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.15,horizontal_flip=True,fill_mode="nearest")
    #where to save the weights
    weights_file="weights/teste.h5"
    #An approach to learning rate reducing through training
    lr_reducer= ReduceLROnPlateau(monitor='val_loss', factor=np.sqrt(0.1), cooldown=0, patience=10, min_lr=0.5e-6)
    #An approach to stop training before the whole epochs are processed
    early_stopper=EarlyStopping(monitor='val_acc', min_delta=0.0001, patience=20)
    #Policy to save weights
    model_checkpoint= ModelCheckpoint(weights_file, monitor="val_acc", save_best_only=True, save_weights_only=True,mode='auto')
    #callbacks
    callbacks=[lr_reducer,early_stopper,model_checkpoint]

    print("Compiling the network")
    #Load model and prepare it for fine tuning
    baseModel = ResNet50(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3)))
    # construct the head of the model that will be placed on top of the
    # the base model
    headModel = baseModel.output
    headModel = Flatten(name="flatten")(headModel)
    headModel = Dense(512, activation="relu")(headModel)
    headModel = Dropout(0.5)(headModel)
    headModel = Dense(num_classes, activation="softmax")(headModel)
    
    # place the head FC model on top of the base model (this will become
    # the actual model we will train)
    model = Model(inputs=baseModel.input, outputs=headModel)
    
    for layer in baseModel.layers:
        layer.trainable = False

    model.compile(loss="categorical_crossentropy", optimizer=adam, metrics=["accuracy"])
    
    trainAug.fit(x_train)
    # Fit the model on the batches generated by datagen.flow().
    print("[INFO] training head...")
    H=model.fit_generator(trainAug.flow(x_train, y_train, batch_size=batch_size), steps_per_epoch=x_train.shape[0] // batch_size, epochs=epochs, validation_data=(x_validation, y_validation), callbacks=callbacks, verbose=2)
    
      if __name__ == "__main__":
    finetune_test_resnet("../224x224-DL-Data/train1-classes.npy", "../224x224-DL-Data/train1-features.npy", "../../5x2_datav2/test1.csv", "train1-test1-predictions.csv" )
从上面的代码中可以看到,我直接从.npy文件读取数据x和y。x\u列文件是约6GB的.npy文件(我的GPU有8GB的RAM),不幸的是,我的脚本已被杀死。但是,当我通过CPU运行它时,脚本也被终止(我的CPU有32GB的RAM)。

因此,我想知道从.npy文件保存和加载数据是否比动态创建它们更消耗RAM,或者是否有任何“Pythonic”方法来克服我的内存限制,或者我是否真的应该减少数据