質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

4363閲覧

image_utilsのimportがうまくいきません

shligeto

総合スコア8

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2019/07/31 04:56

前提・実現したいこと

「直感DeepLearning」という本に記載されているサンプルコード
を実行した際に発生したエラーを解消させたいです.

https://github.com/oreilly-japan/deep-learning-with-keras-ja/tree/master/ch04
の中の example_gan_convolutional.py
というファイルです.

発生している問題・エラーメッセージ

ImportError: cannot import name 'dim_ordering_fix' from 'image_utils' (\Users(ユーザー名)\AppData\Local\Programs\Python\Python37\Lib\site-packages\image_utils\__init__.py)

該当のソースコード

Python3

1import os 2import pandas as pd 3import numpy as np 4import matplotlib as mpl 5import keras.backend as K 6from keras.layers import Flatten, Dropout, LeakyReLU, Input, Activation, Dense, BatchNormalization 7from keras.layers.convolutional import UpSampling2D, Conv2D 8from keras.models import Model 9from keras.optimizers import Adam 10from keras.callbacks import TensorBoard 11from keras.datasets import mnist 12from keras_adversarial.image_grid_callback import ImageGridCallback 13from keras_adversarial import AdversarialModel, simple_gan, gan_targets 14from keras_adversarial import AdversarialOptimizerSimultaneous, normal_latent_sampling 15from image_utils import dim_ordering_fix, dim_ordering_input, dim_ordering_reshape, dim_ordering_unfix 16 17 18# This line allows mpl to run with no DISPLAY defined 19mpl.use("Agg") 20 21 22def model_generator(): 23 nch = 256 24 g_input = Input(shape=[100]) 25 H = Dense(nch * 14 * 14)(g_input) 26 H = BatchNormalization()(H) 27 H = Activation("relu")(H) 28 H = dim_ordering_reshape(nch, 14)(H) 29 H = UpSampling2D(size=(2, 2))(H) 30 H = Conv2D(int(nch / 2), (3, 3), padding="same")(H) 31 H = BatchNormalization()(H) 32 H = Activation("relu")(H) 33 H = Conv2D(int(nch / 4), (3, 3), padding="same")(H) 34 H = BatchNormalization()(H) 35 H = Activation("relu")(H) 36 H = Conv2D(1, (1, 1), padding="same")(H) 37 g_V = Activation("sigmoid")(H) 38 return Model(g_input, g_V) 39 40 41def model_discriminator(input_shape=(1, 28, 28), dropout_rate=0.5): 42 d_input = dim_ordering_input(input_shape, name="input_x") 43 nch = 512 44 # nch = 128 45 H = Conv2D(int(nch / 2), (5, 5), 46 strides=(2, 2), 47 padding="same", 48 activation="relu", 49 )(d_input) 50 H = LeakyReLU(0.2)(H) 51 H = Dropout(dropout_rate)(H) 52 H = Conv2D(nch, (5, 5), 53 strides=(2, 2), 54 padding="same", 55 activation="relu", 56 )(H) 57 H = LeakyReLU(0.2)(H) 58 H = Dropout(dropout_rate)(H) 59 H = Flatten()(H) 60 H = Dense(int(nch / 2))(H) 61 H = LeakyReLU(0.2)(H) 62 H = Dropout(dropout_rate)(H) 63 d_V = Dense(1, activation="sigmoid")(H) 64 return Model(d_input, d_V) 65 66 67def mnist_process(x): 68 x = x.astype(np.float32) / 255.0 69 return x 70 71 72def mnist_data(): 73 (xtrain, ytrain), (xtest, ytest) = mnist.load_data() 74 return mnist_process(xtrain), mnist_process(xtest) 75 76 77def generator_sampler(latent_dim, generator): 78 def fun(): 79 zsamples = np.random.normal(size=(10 * 10, latent_dim)) 80 gen = dim_ordering_unfix(generator.predict(zsamples)) 81 return gen.reshape((10, 10, 28, 28)) 82 83 return fun 84 85 86if __name__ == "__main__": 87 # z \in R^100 88 latent_dim = 100 89 # x \in R^{28x28} 90 input_shape = (1, 28, 28) 91 92 # generator (z -> x) 93 generator = model_generator() 94 # discriminator (x -> y) 95 discriminator = model_discriminator(input_shape=input_shape) 96 # gan (x - > yfake, yreal), z generated on GPU 97 gan = simple_gan(generator, discriminator, 98 normal_latent_sampling((latent_dim,))) 99 100 # print summary of models 101 generator.summary() 102 discriminator.summary() 103 gan.summary() 104 105 # build adversarial model 106 model = AdversarialModel(base_model=gan, 107 player_params=[generator.trainable_weights, 108 discriminator.trainable_weights], 109 player_names=["generator", "discriminator"]) 110 model.adversarial_compile(adversarial_optimizer=AdversarialOptimizerSimultaneous(), 111 player_optimizers=[Adam(1e-4, decay=1e-4), 112 Adam(1e-3, decay=1e-4)], 113 loss="binary_crossentropy") 114 115 # train model 116 generator_cb = ImageGridCallback("output/gan_convolutional/epoch-{:03d}.png", 117 generator_sampler(latent_dim, generator)) 118 callbacks = [generator_cb] 119 if K.backend() == "tensorflow": 120 callbacks.append( 121 TensorBoard(log_dir=os.path.join("output/gan_convolutional/", "logs/"), 122 histogram_freq=0, write_graph=True, write_images=True)) 123 124 xtrain, xtest = mnist_data() 125 xtrain = dim_ordering_fix(xtrain.reshape((-1, 1, 28, 28))) 126 xtest = dim_ordering_fix(xtest.reshape((-1, 1, 28, 28))) 127 y = gan_targets(xtrain.shape[0]) 128 ytest = gan_targets(xtest.shape[0]) 129 history = model.fit(x=xtrain, y=y, validation_data=(xtest, ytest), 130 callbacks=[generator_cb], epochs=100, 131 batch_size=32) 132 df = pd.DataFrame(history.history) 133 df.to_csv("output/gan_convolutional/history.csv") 134 135 generator.save("output/gan_convolutional/generator.h5") 136 discriminator.save("output/gan_convolutional/discriminator.h5") 137

試したこと

自分自身,プログラムについて初心者なので何をすれば良いかわかりませんでした.
とりあえずエラーコードに書いてあるパスに沿って___int__.pyを開いてみましたが,
何も書かれていませんでした.ただ,それが問題なのかどうかもわかりません.

補足情報(FW/ツールのバージョンなど)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ご指定のgithubに、
image_utils.py
というファイルもあると思います。
いま、実行されている環境に、このファイルが置かれていないのでは?

問題となっている関数は、このファイル内で、定義されている関数なので、このファイルもあれば、問題なく、importされると思います。

投稿2019/08/01 00:05

0kcal

総合スコア275

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shligeto

2019/08/01 00:17

今までフォルダの中から 自分がいつも.pyファイルを置いている所に移して実行していました。 フォルダ内に置いてある方を実行したら突破することが出来ました。 非常に助かりました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問