GPU 无法在 tensorflow 中训练神经网络

时间:2021-06-08 10:34:07

标签: python tensorflow keras gpu

我想我正确安装了 tensorflow、CUDA 和 cdNN,以便能够使用 GPU 编译卷积神经网络。 我不知道发生了什么。控制台告诉我

Successfully opened dynamic library cudart64_110.dll

Loaded cuDNN version 8101

但是反正学习时间很长,和colab相比,不知道为什么。 在 Pycharm 编译器中(第一步):199s 2s/step 在 colab 中(第一步):42s 55ms/step 会是什么呢?这是我的代码:

import numpy as np
#import os
#import PIL
#import PIL.Image
import tensorflow as tf
from matplotlib import image
import matplotlib.pyplot as plt
import cv2
import skimage
print(tf.config.experimental.list_physical_devices('GPU'))
#from sklearn.metrics import classification_report, confusion_matrix
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

from keras import backend as K

from keras import backend as K
K._get_available_gpus()

import keras
config = tf.compat.v1.ConfigProto( device_count = {'GPU': 1 , 'CPU': 56} )
sess = tf.compat.v1.Session(config=config)
keras.backend.set_session(sess)


path = "TOMProjekt/Dataset_BUSI_with_GT"
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
H = 400 #wysokosc
W = 400 #szerokosc

path_benign = path + "/benign"

L_benign = 437 #długość zbioru obrazów
h_benign = H #wysokość obrazka
w_benign = W #szerokość obrazka
L_designed = 500 #Docelowa ilość obrazków

labels_benign = np.hstack(((np.ones((L_designed, 1)), np.zeros((L_designed, 2)))))
labels_benign[:,[1,0]] = labels_benign[:,[0,1]]

## łagodny

image_benign = np.zeros((h_benign, w_benign, L_designed))
for i in np.arange(0,L_benign):
  p = path_benign+"/benign ("+str(i+1)+").png"
  img = cv2.resize(image.imread(p)[:,:,0], dsize=(400, 400), interpolation=cv2.INTER_CUBIC) #Skalowanie do 400x400
  #print(np.mean(img))
  img = (img - np.mean(img))/np.std(img) #Normalizacja danych
  #print(i)
  if np.isnan(img).any()==True:
    print("ERROR")
    break
  image_benign[:,:,i] = (img)

for i in np.arange(L_benign,L_designed):
  if i<=L_benign*2:
    img = cv2.GaussianBlur(image_benign[:,:,i-L_benign], (11,11),0)
  else:
    img = skimage.util.random_noise(image_benign[:,:,i-2*L_benign])
  #print(i)
  img = (img - np.mean(img)) / np.std(img)  # Normalizacja danych
  if np.isnan(img).any() == True:
    print("ERROR")
    break
  image_benign[:,:,i] = (img)

## Złośliwy

path_malignant = path + "/malignant"

L_malignant = 210 #długość zbioru obrazów
h_malignant = H #wysokość obrazka
w_malignant = W #szerokość obrazka
L_designed = 500 #Docelowa ilość obrazków

labels_malignant = np.hstack(((np.ones((L_designed, 1)), np.zeros((L_designed, 2)))))
labels_malignant[:,[2,0]] = labels_malignant[:,[0,2]]

image_malignant = np.zeros((h_malignant, w_malignant, L_designed))
for i in np.arange(0,L_malignant):
  #print(i)
  p = path_malignant+"/malignant ("+str(i+1)+").png"
  img = cv2.resize(image.imread(p)[:,:,0], dsize=(400, 400), interpolation=cv2.INTER_CUBIC)
  img = (img - np.mean(img)) / np.std(img)  # Normalizacja danych
  if np.isnan(img).any()==True:
    print("ERROR")
    break
  image_malignant[:,:,i] = (img)

for i in np.arange(L_malignant,L_designed):
  if i<=L_malignant*2:
    img = cv2.GaussianBlur(image_malignant[:,:,i-L_malignant], (11,11),0)
  else:
    img = skimage.util.random_noise(image_malignant[:,:,i-2*L_malignant])
  #print(i)
  img = (img - np.mean(img)) / np.std(img)  # Normalizacja danych
  if np.isnan(img).any()==True:
    print("ERROR")
    break
  image_malignant[:,:,i] = (img)

## BRAK

path_normal = path + "/normal"

L_normal = 133 #długość zbioru obrazów
h_normal = H #wysokość obrazka
w_normal = W #szerokość obrazka
L_designed = 500 #Docelowa ilość obrazków

labels_normal = np.hstack(((np.ones((L_designed, 1)), np.zeros((L_designed, 2)))))

image_normal = np.zeros((h_normal, w_normal, L_designed))
for i in np.arange(0,L_normal):
  p = path_normal+"/normal ("+str(i+1)+").png"
  img = cv2.resize(image.imread(p)[:,:,0], dsize=(400, 400), interpolation=cv2.INTER_CUBIC)
  #print(i)
  img = (img - np.mean(img)) / np.std(img)  # Normalizacja danych
  if np.isnan(img).any() == True:
    print("ERROR")
    break
  image_normal[:,:,i] = (img)

for i in np.arange(L_normal,L_designed):
  if i<=L_normal*2:
    img = cv2.GaussianBlur(image_normal[:,:,i-L_normal], (11,11),0)
  else:
    img = skimage.util.random_noise(image_normal[:,:,i-2*L_normal])
  #print(i)
  if np.isnan(img).any() == True:
    print("ERROR")
    break
  image_normal[:,:,i] = (img)

##Formowanie aby można było je dać na wejście tensora:

print(image_malignant.shape)
image_normal = image_normal.swapaxes(0,2)
image_benign = image_benign.swapaxes(0,2)
image_malignant = image_malignant.swapaxes(0,2)
labels = np.concatenate(([labels_normal, labels_benign, labels_malignant]))
images = np.concatenate(([image_normal, image_benign, image_malignant]))

data = list(zip(labels, images))
np.random.shuffle(data)
new_labels, new_images = zip(*data)
new_labels = np.asarray(new_labels)
new_images = np.asarray(new_images)

Length = new_images.shape[0]

train_ratio = 0.7

images_train = new_images[0:int(0.7*Length), :, :]

images_test = new_images[int(0.7*Length):, :, :]

labels_train = new_labels[0:int(0.7*Length), :]

labels_test = new_labels[int(0.7*Length):, :]

#GoogleNet

import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras import datasets, layers, models, losses, Model

def inception(x,
              filters_1x1,
              filters_3x3_reduce,
              filters_3x3,
              filters_5x5_reduce,
              filters_5x5,
              filters_pool):
  path1 = layers.Conv2D(filters_1x1, (1, 1), padding='same', activation='relu')(x)
  path2 = layers.Conv2D(filters_3x3_reduce, (1, 1), padding='same', activation='relu')(x)
  path2 = layers.Conv2D(filters_3x3, (1, 1), padding='same', activation='relu')(path2)
  path3 = layers.Conv2D(filters_5x5_reduce, (1, 1), padding='same', activation='relu')(x)
  path3 = layers.Conv2D(filters_5x5, (1, 1), padding='same', activation='relu')(path3)
  path4 = layers.MaxPool2D((3, 3), strides=(1, 1), padding='same')(x)
  path4 = layers.Conv2D(filters_pool, (1, 1), padding='same', activation='relu')(path4)
  return tf.concat([path1, path2, path3, path4], axis=3)


inp = layers.Input(shape=(400, 400, 1))
x = layers.Conv2D(64, 7, strides=2, padding='same', activation='relu')(inp)
x = layers.MaxPooling2D(3, strides=2)(x)
x = layers.Conv2D(64, 1, strides=1, padding='same', activation='relu')(x)
x = layers.Conv2D(192, 3, strides=2, padding='same', activation='relu')(x)
x = layers.MaxPooling2D(3, strides=2)(x)
x = inception(x, filters_1x1=64, filters_3x3_reduce=96, filters_3x3=128, filters_5x5_reduce=16, filters_5x5=32,
              filters_pool=32)
x = inception(x, filters_1x1=128, filters_3x3_reduce=128, filters_3x3=192, filters_5x5_reduce=32, filters_5x5=96,
              filters_pool=64)
x = layers.MaxPooling2D(3, strides=2)(x)
x = inception(x, filters_1x1=192, filters_3x3_reduce=96, filters_3x3=208, filters_5x5_reduce=16, filters_5x5=48,
              filters_pool=64)
aux1 = layers.AveragePooling2D((5, 5), strides=3)(x)
aux1 = layers.Conv2D(128, 1, padding='same', activation='relu')(aux1)
aux1 = layers.Flatten()(aux1)
aux1 = layers.Dense(1024, activation='relu')(aux1)
aux1 = layers.Dropout(0.7)(aux1)
aux1 = layers.Dense(10, activation='softmax')(aux1)
x = inception(x, filters_1x1=160, filters_3x3_reduce=112, filters_3x3=224, filters_5x5_reduce=24, filters_5x5=64,
              filters_pool=64)
x = inception(x, filters_1x1=128, filters_3x3_reduce=128, filters_3x3=256, filters_5x5_reduce=24, filters_5x5=64,
              filters_pool=64)
x = inception(x, filters_1x1=112, filters_3x3_reduce=144, filters_3x3=288, filters_5x5_reduce=32, filters_5x5=64,
              filters_pool=64)
aux2 = layers.AveragePooling2D((5, 5), strides=3)(x)
aux2 = layers.Conv2D(128, 1, padding='same', activation='relu')(aux2)
aux2 = layers.Flatten()(aux2)
aux2 = layers.Dense(1024, activation='relu')(aux2)
aux2 = layers.Dropout(0.7)(aux2)
aux2 = layers.Dense(10, activation='softmax')(aux2)
x = inception(x, filters_1x1=256, filters_3x3_reduce=160, filters_3x3=320, filters_5x5_reduce=32, filters_5x5=128,
              filters_pool=128)
x = layers.MaxPooling2D(3, strides=2)(x)
x = inception(x, filters_1x1=256, filters_3x3_reduce=160, filters_3x3=320, filters_5x5_reduce=32, filters_5x5=128,
              filters_pool=128)
x = inception(x, filters_1x1=384, filters_3x3_reduce=192, filters_3x3=384, filters_5x5_reduce=48, filters_5x5=128,
              filters_pool=128)
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dropout(0.4)(x)
out = layers.Dense(3, activation='softmax')(x)

model = tf.keras.Model(inp, out, name="googlenet")
model.compile(
  optimizer=tf.keras.optimizers.RMSprop(1e-3),
  loss=tf.keras.losses.binary_crossentropy
)

history = model.fit(images_train, labels_train, batch_size=10, epochs=20, validation_split=0.2)

0 个答案:

没有答案