question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OSError: Unable to open file (unable to open file: name = 'C:\Users\(name blocked)\Scripts\Neural_Network.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)

See original GitHub issue
  • Windows 10
  • Python version 3.7
  • Miniconda
  • h5py version: latest I was trying to load a model with Keras and train it again.

Here is my code:

import gym
import random
import numpy as np
import tflearn
import os
import h5py
import tensorflow as tf
from tensorflow import keras
from keras.models import load_model
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
from statistics import mean, median
from collections import Counter

LR = 1e-3

env = gym.make('CartPole-v0')
env.reset()

goal_steps = 300

score_requirement = 50

initial_games = 10000

def some_random_games_first():
    for episode in range (5):
        env.reset()
        for t in range(goal_steps):
            env.render()
            action = env.action_space.sample()
            observation, reward, done, info = env.step(action)
            if done:
                break

def intial_population():
	training_data = []
	scores = []
	accepted_scores = [] 
	for _ in range (initial_games):
		score = 0
		game_memory = []
		prev_observation = []
		for _ in range (goal_steps):
			action = random.randrange (0,2)
			observation, reward, done, info = env.step(action)

			if len(prev_observation) > 0:
				game_memory.append([prev_observation, action])

			prev_observation = observation
			score += reward
			if done:
				break
		if score >= score_requirement:
			accepted_scores.append(score)
			for data in game_memory:
				if data [1] == 1:
					output = [0, 1]
				elif data [1] == 0:
					output = [1, 0]

				training_data.append([data[0], output])
		env.reset()
		scores.append(score)
	training_data_save = np.array (training_data)
	np.save ("saved.npy", training_data_save)
	print ("Average accepted scores", mean(accepted_scores))
	print("Median accepted score: ", median(accepted_scores))
	print (Counter (accepted_scores))

	return training_data
intial_population()

def neural_network_model (input_size):
	network = input_data (shape=[None, input_size, 1], name="input")
	network = fully_connected(network, 128, activation="relu")
	network = dropout(network, 0.8)

	network = fully_connected(network, 256, activation="relu")
	network = dropout(network, 0.8)

	network = fully_connected(network, 512, activation="relu")
	network = dropout(network, 0.8)

	network = fully_connected(network, 256, activation="relu")
	network = dropout(network, 0.8)

	network = fully_connected(network, 128, activation="relu")
	network = dropout(network, 0.8)

	network = fully_connected(network, 2, activation="softmax")
	network = regression(network, optimizer="adam", learning_rate = LR, loss = "categorical_crossentropy", name = 'targets')
	model = tflearn.DNN (network, tensorboard_dir='log")

	return model

def train_model(training_data, model=False):
	X = np.array ([i [0] for i in training_data]).reshape(-1, len(training_data[0][0]), 1)
	y = [i [1] for i in training_data]

	if not model:
		model = neural_network_model (input_size = len(X[0]))
	model.fit({'input' :X}, {'targets' :y}, n_epoch=3, snapshot_step=500, show_metric=True,
		run_id='openaistuff')
	return model

training_data = intial_population()
model = train_model(training_data)


scores = [] 
choices = []

for each_game in range(1):
	score = 0
	game_memory= []
	prev_obs = []
	env.reset()
	for _ in range (goal_steps):
		env.render()
		if len(prev_obs) == 0:
			action = random.randrange (0,2)
		else:
			action = np.argmax(model.predict(prev_obs.reshape(-1, len(prev_obs),1)) [0])
		choices.append(action)

		new_observation, reward, done, info = env.step(action)
		prev_obs = new_observation
		game_memory.append ([new_observation, action])
		score += reward
		if done:
			break
	scores.append(score)
	model.save("C:\\Users\\William\\Scripts\\Neural_Network.h5")
Average_Score = sum(scores)/len(scores)
print("Average Score: ", Average_Score)
print("choice 1:{}  choice 0:{}".format(choices.count(1)/len(choices),choices.count(0)/len(choices)))
print(score_requirement)
if Average_Score > 299:
	print("Solved")
else:
	print("Not solved, trying again")
	new_model = load_model("C:\\Users\\William\\Scripts\\Neural_Network.h5")'

And when I would run the program I would get this error:

Traceback (most recent call last):
  File "C:\Users\William\Scripts\Cartpole.py", line 157, in <module>
    new_model = load_model('C:\\Users\\William\\Scripts\\Neural_Network.h5')
  File "C:\Users\William\Miniconda3\envs\tensorflow\lib\site-packages\keras\engine\saving.py", line 417, in load_model
    f = h5dict(filepath, 'r')
  File "C:\Users\William\Miniconda3\envs\tensorflow\lib\site-packages\keras\utils\io_utils.py", line 186, in __init__
    self.data = h5py.File(path, mode=mode)
  File "C:\Users\William\Miniconda3\envs\tensorflow\lib\site-packages\h5py\_hl\files.py", line 394, in __init__
    swmr=swmr)
  File "C:\Users\William\Miniconda3\envs\tensorflow\lib\site-packages\h5py\_hl\files.py", line 170, in make_fid
    fid = h5f.open(name, flags, fapl=fapl)
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py\h5f.pyx", line 85, in h5py.h5f.open
OSError: Unable to open file (unable to open file: name = 'C:\Users\William\Scripts\Neural_Network.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)

I have looked at the file and it generates the files: checkpoint,(thing used for the file type ‘file’), Neural_Network.h5.data-00000-of-00001, Neural_Network.h5, and Neural_Network.h5.meta. I see the ‘Neural_Network.h5’ so I am not sure why it cannot load it. Any answers are appreciated!

Edit: sorry for bad code formatting I saw it and cannot seem to fix it

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:24 (5 by maintainers)

github_iconTop GitHub Comments

12reactions
IvanEvancommented, Mar 31, 2020
  1. If you run a.py got the error, but you load the model.h5 in b.py
  2. Please cp model.h5 to the same level directory with a.py
  3. Edit b.py’s load(‘xx/model.h5’) to load(‘model.h5’)

It worked for me.

12reactions
mechlyacommented, Mar 24, 2020

try to replace the Backslash ‘' in your path by Forward slash ’ /’. C:\Users\William\Scripts\Neural_Network.h5 =============> C:/Users\William/Scripts/Neural_Network.h5

Read more comments on GitHub >

github_iconTop Results From Across the Web

OSError: Unable to open file (unable to open file)
OSError : Unable to open file (unable to open file: name = '', errno = 2, error message = ' No such file...
Read more >
name = 'weight_res1.h5', errno = 2, error message = 'No such ...
OSError : Unable to open file (unable to open file: name = 'weight_res1.h5', errno = 2, error message = 'No such file or...
Read more >
Developers - OSError - Bountysource
OSError : Unable to open file (unable to open file: name = 'C:\Users\(name blocked)\Scripts\Neural_Network.h5', errno = 2, error message ...
Read more >
Python FileNotFoundError: [Errno 2] No such file or directory ...
This is, of course, unless you are creating a new file and writing to it. If you reference a file that does not...
Read more >
[Errno 2] No such file or directory " in PyCharm. - YouTube
In this video, I will show you how to fix : " Can't open file ' file.py': [ Errno 2 ] No such...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found