teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

修正

2021/11/22 12:58

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -18,7 +18,14 @@
18
18
  from tensorflow.keras.applications.vgg16 import preprocess_input
19
19
  from tensorflow.keras.models import Model
20
20
  from tensorflow.keras.models import load_model
21
-
21
+ #tokenizerをloadする
22
+ tokenizer = load(open('tokenizer1.pkl', 'rb'))
23
+ #trainigしたときのpre-defineでmax sequence lengthの値
24
+ max_length = 43
25
+ #modelのload
26
+ model = load_model('model-ep004-loss2.778-val_loss3.210.h5')
27
+ #text_readingに使われる画像のpath
28
+ text_reading_image_path ='image2.png'
22
29
  class Image_captioning: #説明文を生成するクラス
23
30
  # extract features from each photo in the directory
24
31
  def extract_features(self,filename):
@@ -47,6 +54,47 @@
47
54
  feature = model.predict(image, verbose=0)
48
55
  print('i')
49
56
  return feature
57
+ # map an integer to a word
58
+ def word_for_id(self,integer, tokenizer):
59
+ for word, index in tokenizer.word_index.items():
60
+ if index == integer:
61
+ return word
62
+ return None
63
+
64
+ # generate a description for an image
65
+ def generate_desc(self,model, tokenizer, photo, max_length):
66
+ # seed the generation process
67
+ in_text = 'startseq'
68
+ # iterate over the whole length of the sequence
69
+ for i in range(max_length):
70
+ # integer encode input sequence
71
+ sequence = tokenizer.texts_to_sequences([in_text])[0]
72
+ # pad input
73
+ sequence = pad_sequences([sequence], maxlen=max_length)
74
+ # predict next word
75
+ yhat = model.predict([photo,sequence], verbose=0)
76
+ # convert probability to integer
77
+ yhat = argmax(yhat)
78
+ # map integer to word
79
+ word = self.word_for_id(yhat, tokenizer)
80
+ # stop if we cannot map the word
81
+ if word is None:
82
+ break
83
+ # append as input for generating the next word
84
+ in_text += ' ' + word
85
+ # stop if we predict the end of the sequence
86
+ if word == 'endseq':
87
+ break
88
+ return in_text
89
+ def text_reading(self):
90
+ photo = self.extract_features(text_reading_image_path)
91
+ d = self.generate_desc(model, tokenizer, photo, max_length)
92
+ description = d.replace('startseq',' ',1).replace('endseq',' ',1)
93
+ print(description)
94
+ sound = gTTS(text=description,lang='ja',slow=False)
95
+ sound.save('/home/limlab/program/navigation/potential/voice/navigation.mp3')
96
+ playsound('/home/limlab/program/voice/1.wav')
97
+ playsound("/home/limlab/program/navigation/potential/voice/navigation.mp3")
50
98
  #ここからはエラーメッセージとなります
51
99
  a
52
100
  2021-11-21 11:58:17.479209: I tensorflow/stream_executor/cuda/cuda_driver.cc:775] failed to allocate 1.00G (1073741824 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory

2

モジュールのインポートを追加

2021/11/22 12:58

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -11,6 +11,14 @@
11
11
 
12
12
  ### コードとエラーメッセージになります
13
13
  ```
14
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
15
+ from tensorflow.keras.applications.vgg16 import VGG16
16
+ from tensorflow.keras.preprocessing.image import load_img
17
+ from tensorflow.keras.preprocessing.image import img_to_array
18
+ from tensorflow.keras.applications.vgg16 import preprocess_input
19
+ from tensorflow.keras.models import Model
20
+ from tensorflow.keras.models import load_model
21
+
14
22
  class Image_captioning: #説明文を生成するクラス
15
23
  # extract features from each photo in the directory
16
24
  def extract_features(self,filename):

1

修正

2021/11/21 15:42

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -9,30 +9,63 @@
9
9
  お手数をおかけしますが宜しくおねがい致します。
10
10
 
11
11
 
12
- ### 発生している問題・エラーメッセージ
12
+ ### コードとエラーメッセージになります
13
-
14
13
  ```
15
- 2021-11-19 19:55:06.734805: I tensorflow/stream_executor/cuda/cuda_driver.cc:775] failed to allocate 1.00G (1073741824 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
16
- 2021-11-19 19:55:06.735052: I tensorflow/stream_executor/cuda/cuda_driver.cc:775] failed to allocate 921.60M (966367744 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
17
- 2021-11-19 19:55:06.735269: I tensorflow/stream_executor/cuda/cuda_driver.cc:775] failed to allocate 829.44M (869731072 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
18
- 2021-11-19 19:55:06.735488: I tensorflow/stream_executor/cuda/cuda_driver.cc:775] failed to allocate 746.50M (782758144 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
19
- 2021-11-19 19:55:06.735701: I tensorflow/stream_executor/cuda/cuda_driver.cc:775] failed to allocate 671.85M (704482304 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
20
- 2021-11-19 19:55:06.735926: I tensorflow/stream_executor/cuda/cuda_driver.cc:775] failed to allocate 604.66M (634034176 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
21
- 2021-11-19 19:55:06.736143: I tensorflow/stream_executor/cuda/cuda_driver.cc:775] failed to allocate 544.20M (570630912 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
22
- 2021-11-19 19:55:07.551528: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 411041792 exceeds 10% of free system memory.
14
+ class Image_captioning: #説明文を生成するクラス
15
+ # extract features from each photo in the directory
16
+ def extract_features(self,filename):
17
+ print('a')
18
+ # load the model
19
+ model = VGG16()
20
+ print('b')
21
+ # re-structure the model
22
+ model.layers.pop()
23
+ print('c')
24
+ model = Model(inputs=model.inputs, outputs=model.layers[-1].output)
25
+ print('d')
26
+ # load the photo
27
+ image = load_img(filename, target_size=(224, 224))
28
+ print('e')
29
+ # convert the image pixels to a numpy array
30
+ image = img_to_array(image)
31
+ print('f')
32
+ # reshape data for the model
33
+ image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
34
+ print('g')
35
+ # prepare the image for the VGG model
36
+ image = preprocess_input(image)
37
+ print("h")
38
+ # get features
39
+ feature = model.predict(image, verbose=0)
40
+ print('i')
41
+ return feature
42
+ #ここからはエラーメッセージとなります
43
+ a
44
+ 2021-11-21 11:58:17.479209: I tensorflow/stream_executor/cuda/cuda_driver.cc:775] failed to allocate 1.00G (1073741824 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
45
+ 2021-11-21 11:58:17.479540: I tensorflow/stream_executor/cuda/cuda_driver.cc:775] failed to allocate 921.60M (966367744 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
46
+ 2021-11-21 11:58:17.479817: I tensorflow/stream_executor/cuda/cuda_driver.cc:775] failed to allocate 829.44M (869731072 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
47
+ 2021-11-21 11:58:17.480066: I tensorflow/stream_executor/cuda/cuda_driver.cc:775] failed to allocate 746.50M (782758144 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
48
+ 2021-11-21 11:58:17.480313: I tensorflow/stream_executor/cuda/cuda_driver.cc:775] failed to allocate 671.85M (704482304 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
49
+ 2021-11-21 11:58:17.480559: I tensorflow/stream_executor/cuda/cuda_driver.cc:775] failed to allocate 604.66M (634034176 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
50
+ 2021-11-21 11:58:18.318427: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 411041792 exceeds 10% of free system memory.
51
+ b
52
+ c
53
+ d
54
+ e
55
+ f
56
+ g
57
+ h
58
+ 2021-11-21 11:58:18.680658: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcublas.so.10
59
+ 2021-11-21 11:58:18.779763: E tensorflow/stream_executor/cuda/cuda_blas.cc:225] failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED
60
+ 2021-11-21 11:58:18.781533: E tensorflow/stream_executor/cuda/cuda_blas.cc:225] failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED
61
+ 2021-11-21 11:58:18.782176: E tensorflow/stream_executor/cuda/cuda_blas.cc:225] failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED
62
+ 2021-11-21 11:58:18.782845: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudnn.so.7
63
+ 2021-11-21 11:58:18.785736: E tensorflow/stream_executor/cuda/cuda_dnn.cc:328] Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
64
+ 2021-11-21 11:58:18.786488: E tensorflow/stream_executor/cuda/cuda_dnn.cc:328] Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
65
+ 2021-11-21 11:58:18.786504: W tensorflow/core/framework/op_kernel.cc:1767] OP_REQUIRES failed at conv_ops_fused_impl.h:642 : Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
23
66
 
24
- 2021-11-19 19:55:07.882426: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcublas.so.10
25
- 2021-11-19 19:55:07.937269: E tensorflow/stream_executor/cuda/cuda_blas.cc:225] failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED
26
- 2021-11-19 19:55:07.938436: E tensorflow/stream_executor/cuda/cuda_blas.cc:225] failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED
27
- 2021-11-19 19:55:07.939484: E tensorflow/stream_executor/cuda/cuda_blas.cc:225] failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED
28
- 2021-11-19 19:55:07.940272: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudnn.so.7
29
- 2021-11-19 19:55:07.942591: E tensorflow/stream_executor/cuda/cuda_dnn.cc:328] Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
30
- 2021-11-19 19:55:07.943814: E tensorflow/stream_executor/cuda/cuda_dnn.cc:328] Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
31
- 2021-11-19 19:55:07.943831: W tensorflow/core/framework/op_kernel.cc:1767] OP_REQUIRES failed at conv_ops_fused_impl.h:642 : Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
32
-
33
67
  ```
34
68
 
35
-
36
69
  ### 試したこと
37
70
  下記のコードを挿入し改善しようとしました。
38
71