@@ -148,7 +148,7 @@ def load_checkpoint(checkpoint, model, optimizer=None):
148148 optimizer: (torch.optim) optional: resume optimizer from checkpoint
149149 """
150150 if not os .path .exists (checkpoint ):
151- raise ("File doesn't exist {}" .format (checkpoint ))
151+ raise (FileNotFoundError ( "File doesn't exist {}" .format (checkpoint ) ))
152152 if torch .cuda .is_available ():
153153 checkpoint = torch .load (checkpoint )
154154 else :
@@ -163,65 +163,35 @@ def load_checkpoint(checkpoint, model, optimizer=None):
163163 return checkpoint
164164
165165
166- class Board_Logger (object ):
167- """Tensorboard log utility"""
168-
166+ class BoardLogger :
167+ """TensorBoard log utility for TensorFlow 2.x"""
169168 def __init__ (self , log_dir ):
170- """Create a summary writer logging to log_dir."""
171- self .writer = tf .summary .FileWriter (log_dir )
169+ self .writer = tf .summary .create_file_writer (log_dir )
172170
173171 def scalar_summary (self , tag , value , step ):
174172 """Log a scalar variable."""
175- summary = tf .Summary (value = [tf .Summary .Value (tag = tag , simple_value = value )])
176- self .writer .add_summary (summary , step )
173+ with self .writer .as_default ():
174+ tf .summary .scalar (tag , value , step = step )
175+ self .writer .flush ()
177176
178177 def image_summary (self , tag , images , step ):
179178 """Log a list of images."""
179+ with self .writer .as_default ():
180+ for i , img in enumerate (images ):
181+ # Convert image to a TensorFlow-compatible format
182+ if isinstance (img , np .ndarray ):
183+ img = tf .convert_to_tensor (img , dtype = tf .uint8 )
184+ if img .ndim == 2 : # Add channel dimension for grayscale images
185+ img = tf .expand_dims (img , axis = - 1 )
186+ tf .summary .image (f"{ tag } /{ i } " , tf .expand_dims (img , 0 ), step = step ) # Add batch dimension
187+ self .writer .flush ()
180188
181- img_summaries = []
182- for i , img in enumerate (images ):
183- # Write the image to a string
184- try :
185- s = StringIO ()
186- except :
187- s = BytesIO ()
188- scipy .misc .toimage (img ).save (s , format = "png" )
189-
190- # Create an Image object
191- img_sum = tf .Summary .Image (encoded_image_string = s .getvalue (),
192- height = img .shape [0 ],
193- width = img .shape [1 ])
194- # Create a Summary value
195- img_summaries .append (tf .Summary .Value (tag = '%s/%d' % (tag , i ), image = img_sum ))
196-
197- # Create and write Summary
198- summary = tf .Summary (value = img_summaries )
199- self .writer .add_summary (summary , step )
200-
201189 def histo_summary (self , tag , values , step , bins = 1000 ):
202190 """Log a histogram of the tensor of values."""
203-
204- # Create a histogram using numpy
205- counts , bin_edges = np .histogram (values , bins = bins )
206-
207- # Fill the fields of the histogram proto
208- hist = tf .HistogramProto ()
209- hist .min = float (np .min (values ))
210- hist .max = float (np .max (values ))
211- hist .num = int (np .prod (values .shape ))
212- hist .sum = float (np .sum (values ))
213- hist .sum_squares = float (np .sum (values ** 2 ))
214-
215- # Drop the start of the first bin
216- bin_edges = bin_edges [1 :]
217-
218- # Add bin edges and counts
219- for edge in bin_edges :
220- hist .bucket_limit .append (edge )
221- for c in counts :
222- hist .bucket .append (c )
223-
224- # Create and write Summary
225- summary = tf .Summary (value = [tf .Summary .Value (tag = tag , histo = hist )])
226- self .writer .add_summary (summary , step )
227- self .writer .flush ()
191+ with self .writer .as_default ():
192+ # Create histogram data using numpy
193+ counts , bin_edges = np .histogram (values , bins = bins )
194+
195+ # Create a histogram summary
196+ tf .summary .histogram (tag , values , step = step )
197+ self .writer .flush ()
0 commit comments