我在Tensorflow中有一些DCGAN的代码,可用于一些简单的数据集。
但是它不适用于我要处理的数据集。
这是完整的代码-和错误:
# coding: utf-8
# # Training a DCGAN to draw fake and real images
# In[1]:
directory = "../Data/image_files/"
new_dir = "../Data/image_files/cropped"
import urllib
import urllib.request
import tarfile
import os
import tarfile
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.image import imread
from scipy.misc import imresize, imsave
import tensorflow as tf
get_ipython().run_line_magic('matplotlib', 'inline')
# ## Modifying the images (reducing their size)
# In[2]:
height_width = 100
filepaths = []
for dir_, _, files in os.walk(directory):
for fileName in files:
relDir = os.path.relpath(dir_, directory)
relFile = os.path.join(relDir, fileName)
filepaths.append(directory + "/" + relFile)
fail_count = 0
for i, fp in enumerate(filepaths):
try:
img = imread(fp, 0) #/ 255.0
img = imresize(img, (height_width, height_width))
imsave(new_dir + "/" + str(i) + ".png", img)
except:
fail_count += 1
#img = imread("../Data/white_square.png", 0) #/ 100 width square
#imsave(new_dir + "/" + str(i) + ".png", img)
#with open("../Data/fail_log.text", "a+") as f:
# f.write(fp)
print(fail_count)
# Load the file names of files into a list
# In[3]:
filepaths_new = []
for dir_, _, files in os.walk(new_dir):
for fileName in files:
if not fileName.endswith(".png"):
continue
relDir = os.path.relpath(dir_, directory)
relFile = os.path.join(relDir, fileName)
filepaths_new.append(directory + "/" + relFile)
# ## Define next batch
# In[4]:
def next_batch(num=64, data=filepaths_new):
idx = np.arange(0 , len(data))
np.random.shuffle(idx)
idx = idx[:num]
print(idx)
data_shuffle = [imread(data[i]) for i in idx]
shuffled = np.asarray(data_shuffle)
print("shuffled:", shuffled.shape)
return np.asarray(data_shuffle)
# ## Code for creating montages (by Parag Mital)
# In[5]:
# Code by Parag Mital (https://github.com/pkmital/CADL/)
def montage(images):
if isinstance(images, list):
images = np.array(images)
img_h = images.shape[1]
img_w = images.shape[2]
n_plots = int(np.ceil(np.sqrt(images.shape[0])))
if len(images.shape) == 4 and images.shape[3] == 3:
m = np.ones(
(images.shape[1] * n_plots + n_plots + 1,
images.shape[2] * n_plots + n_plots + 1, 3)) * 0.5
elif len(images.shape) == 4 and images.shape[3] == 1:
m = np.ones(
(images.shape[1] * n_plots + n_plots + 1,
images.shape[2] * n_plots + n_plots + 1, 1)) * 0.5
elif len(images.shape) == 3:
m = np.ones(
(images.shape[1] * n_plots + n_plots + 1,
images.shape[2] * n_plots + n_plots + 1)) * 0.5
else:
raise ValueError('Could not parse image shape of {}'.format(
images.shape))
for i in range(n_plots):
for j in range(n_plots):
this_filter = i * n_plots + j
if this_filter < images.shape[0]:
this_img = images[this_filter]
m[1 + i + i * img_h:1 + i + (i + 1) * img_h,
1 + j + j * img_w:1 + j + (j + 1) * img_w] = this_img
return m
# ## Definition of the neural network
# In[6]:
tf.reset_default_graph()
batch_size = 5
n_noise = 28
#config = tf.ConfigProto()
#config.gpu_options.allocator_type ='BFC'
#config.gpu_options.per_process_gpu_memory_fraction = 0.90
X_in = tf.placeholder(dtype=tf.float32, shape=[None, height_width, height_width, 3], name='X')
noise = tf.placeholder(dtype=tf.float32, shape=[None, n_noise])
keep_prob = tf.placeholder(dtype=tf.float32, name='keep_prob')
is_training = tf.placeholder(dtype=tf.bool, name='is_training')
def lrelu(x):
return tf.maximum(x, tf.multiply(x, 0.2))
def binary_cross_entropy(x, z):
eps = 1e-12
return (-(x * tf.log(z + eps) + (1. - x) * tf.log(1. - z + eps)))
def discriminator(img_in, reuse=None, keep_prob=keep_prob):
activation = lrelu
with tf.variable_scope("discriminator", reuse=reuse):
x = tf.reshape(img_in, shape=[-1, height_width, height_width, 3])
x = tf.layers.conv2d(x, kernel_size=5, filters=256, strides=2, padding='same', activation=activation)
x = tf.layers.dropout(x, keep_prob)
x = tf.layers.conv2d(x, kernel_size=5, filters=128, strides=1, padding='same', activation=activation)
x = tf.layers.dropout(x, keep_prob)
x = tf.layers.conv2d(x, kernel_size=5, filters=64, strides=1, padding='same', activation=activation)
x = tf.layers.dropout(x, keep_prob)
x = tf.contrib.layers.flatten(x)
x = tf.layers.dense(x, units=128, activation=activation)
x = tf.layers.dense(x, units=1, activation=tf.nn.sigmoid)
return x
def generator(z, keep_prob=keep_prob, is_training=is_training):
activation = lrelu
momentum = 0.9
with tf.variable_scope("generator", reuse=None):
x = z
d1 = 4#3
d2 = 3
x = tf.layers.dense(x, units=d1 * d1 * d2, activation=activation)
x = tf.layers.dropout(x, keep_prob)
x = tf.contrib.layers.batch_norm(x, is_training=is_training, decay=momentum)
x = tf.reshape(x, shape=[-1, d1, d1, d2])
x = tf.image.resize_images(x, size=[10, 10])
x = tf.layers.conv2d_transpose(x, kernel_size=5, filters=256, strides=2, padding='same', activation=activation)
x = tf.layers.dropout(x, keep_prob)
x = tf.contrib.layers.batch_norm(x, is_training=is_training, decay=momentum)
x = tf.layers.conv2d_transpose(x, kernel_size=5, filters=128, strides=2, padding='same', activation=activation)
x = tf.layers.dropout(x, keep_prob)
x = tf.contrib.layers.batch_norm(x, is_training=is_training, decay=momentum)
x = tf.layers.conv2d_transpose(x, kernel_size=5, filters=64, strides=1, padding='same', activation=activation)
x = tf.layers.dropout(x, keep_prob)
x = tf.contrib.layers.batch_norm(x, is_training=is_training, decay=momentum)
x = tf.layers.conv2d_transpose(x, kernel_size=5, filters=3, strides=1, padding='same', activation=tf.nn.sigmoid)
return x
# In[ ]:
# ## Losses and optimizers
# In[7]:
g = generator(noise, keep_prob, is_training)
print(g)
d_real = discriminator(X_in)
d_fake = discriminator(g, reuse=True)
vars_g = [var for var in tf.trainable_variables() if var.name.startswith("generator")]
vars_d = [var for var in tf.trainable_variables() if var.name.startswith("discriminator")]
d_reg = tf.contrib.layers.apply_regularization(tf.contrib.layers.l2_regularizer(1e-6), vars_d)
g_reg = tf.contrib.layers.apply_regularization(tf.contrib.layers.l2_regularizer(1e-6), vars_g)
loss_d_real = binary_cross_entropy(tf.ones_like(d_real), d_real)
loss_d_fake = binary_cross_entropy(tf.zeros_like(d_fake), d_fake)
loss_g = tf.reduce_mean(binary_cross_entropy(tf.ones_like(d_fake), d_fake))
loss_d = tf.reduce_mean(0.5 * (loss_d_real + loss_d_fake))
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
optimizer_d = tf.train.RMSPropOptimizer(learning_rate=0.0001).minimize(loss_d + d_reg, var_list=vars_d)
optimizer_g = tf.train.RMSPropOptimizer(learning_rate=0.0002).minimize(loss_g + g_reg, var_list=vars_g)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# ## Training the network
# In[9]:
for i in range(60000):
train_d = True
train_g = True
keep_prob_train = 0.6 # 0.5
n = np.random.uniform(0.0, 1.0, [batch_size, n_noise]).astype(np.float32)
batch = [b for b in next_batch(num=batch_size)]
d_real_ls, d_fake_ls, g_ls, d_ls = sess.run([loss_d_real, loss_d_fake, loss_g, loss_d], feed_dict={X_in: batch, noise: n, keep_prob: keep_prob_train, is_training:True})
d_fake_ls_init = d_fake_ls
d_real_ls = np.mean(d_real_ls)
d_fake_ls = np.mean(d_fake_ls)
g_ls = g_ls
d_ls = d_ls
if g_ls * 1.35 < d_ls:
train_g = False
pass
if d_ls * 1.35 < g_ls:
train_d = False
pass
if train_d:
sess.run(optimizer_d, feed_dict={noise: n, X_in: batch, keep_prob: keep_prob_train, is_training:True})
if train_g:
sess.run(optimizer_g, feed_dict={noise: n, keep_prob: keep_prob_train, is_training:True})
if not i % 10:
print (i, d_ls, g_ls)
if not train_g:
print("not training generator")
if not train_d:
print("not training discriminator")
gen_imgs = sess.run(g, feed_dict = {noise: n, keep_prob: 1.0, is_training:False})
imgs = [img[:,:,:] for img in gen_imgs]
m = montage(imgs)
#m = imgs[0]
plt.axis('off')
plt.imshow(m, cmap='gray')
plt.show()
错误:
[13156 13051 2944 11114 11884]
shuffled: (5, 100, 100, 3)
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1321 try:
-> 1322 return fn(*args)
1323 except errors.OpError as e:
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
1306 return self._call_tf_sessionrun(
-> 1307 options, feed_dict, fetch_list, target_list, run_metadata)
1308
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
1408 self._session, options, feed_dict, fetch_list, target_list,
-> 1409 run_metadata)
1410 else:
InvalidArgumentError: Input to reshape is a tensor with 24000 values, but the requested shape requires a multiple of 30000
[[Node: discriminator_1/Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](generator/conv2d_transpose_3/Sigmoid, discriminator/Reshape/shape)]]
During handling of the above exception, another exception occurred:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-8-3639a81d708d> in <module>()
8 batch = [b for b in next_batch(num=batch_size)]
9
---> 10 d_real_ls, d_fake_ls, g_ls, d_ls = sess.run([loss_d_real, loss_d_fake, loss_g, loss_d], feed_dict={X_in: batch, noise: n, keep_prob: keep_prob_train, is_training:True})
11
12 d_fake_ls_init = d_fake_ls
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
898 try:
899 result = self._run(None, fetches, feed_dict, options_ptr,
--> 900 run_metadata_ptr)
901 if run_metadata:
902 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1133 if final_fetches or final_targets or (handle and feed_dict_tensor):
1134 results = self._do_run(handle, final_targets, final_fetches,
-> 1135 feed_dict_tensor, options, run_metadata)
1136 else:
1137 results = []
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1314 if handle is None:
1315 return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1316 run_metadata)
1317 else:
1318 return self._do_call(_prun_fn, handle, feeds, fetches)
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1333 except KeyError:
1334 pass
-> 1335 raise type(e)(node_def, op, message)
1336
1337 def _extend_graph(self):
InvalidArgumentError: Input to reshape is a tensor with 24000 values, but the requested shape requires a multiple of 30000
[[Node: discriminator_1/Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](generator/conv2d_transpose_3/Sigmoid, discriminator/Reshape/shape)]]
Caused by op 'discriminator_1/Reshape', defined at:
File "/Users/davidelks/anaconda3/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/Users/davidelks/anaconda3/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/traitlets/config/application.py", line 658, in launch_instance
app.start()
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/ipykernel/kernelapp.py", line 486, in start
self.io_loop.start()
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/tornado/platform/asyncio.py", line 127, in start
self.asyncio_loop.run_forever()
File "/Users/davidelks/anaconda3/lib/python3.6/asyncio/base_events.py", line 422, in run_forever
self._run_once()
File "/Users/davidelks/anaconda3/lib/python3.6/asyncio/base_events.py", line 1432, in _run_once
handle._run()
File "/Users/davidelks/anaconda3/lib/python3.6/asyncio/events.py", line 145, in _run
self._callback(*self._args)
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/tornado/ioloop.py", line 759, in _run_callback
ret = callback()
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/tornado/stack_context.py", line 276, in null_wrapper
return fn(*args, **kwargs)
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 536, in <lambda>
self.io_loop.add_callback(lambda : self._handle_events(self.socket, 0))
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 450, in _handle_events
self._handle_recv()
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 480, in _handle_recv
self._run_callback(callback, msg)
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 432, in _run_callback
callback(*args, **kwargs)
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/tornado/stack_context.py", line 276, in null_wrapper
return fn(*args, **kwargs)
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
handler(stream, idents, msg)
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/ipykernel/ipkernel.py", line 208, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 537, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2662, in run_cell
raw_cell, store_history, silent, shell_futures)
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2785, in _run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2903, in run_ast_nodes
if self.run_code(code, result):
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2963, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-7-72cb0c2de1f2>", line 6, in <module>
d_fake = discriminator(g, reuse=True)
File "<ipython-input-6-81fd05d19ace>", line 25, in discriminator
x = tf.reshape(img_in, shape=[-1, height_width, height_width, 3])
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 6113, in reshape
"Reshape", tensor=tensor, shape=shape, name=name)
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3392, in create_op
op_def=op_def)
File "/Users/davidelks/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1718, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 24000 values, but the requested shape requires a multiple of 30000
[[Node: discriminator_1/Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](generator/conv2d_transpose_3/Sigmoid, discriminator/Reshape/shape)]]