Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Fidle
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Guillaume Gautier
Fidle
Commits
584177e5
Commit
584177e5
authored
4 years ago
by
Jean-Luc Parouty
Browse files
Options
Downloads
Patches
Plain Diff
Update override stuff
parent
51dc516e
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
VAE/08-VAE-with-CelebA.ipynb
+12
-165
12 additions, 165 deletions
VAE/08-VAE-with-CelebA.ipynb
VAE/batch_slurm.sh
+1
-1
1 addition, 1 deletion
VAE/batch_slurm.sh
fidle/pwk.py
+21
-12
21 additions, 12 deletions
fidle/pwk.py
with
34 additions
and
178 deletions
VAE/08-VAE-with-CelebA.ipynb
+
12
−
165
View file @
584177e5
...
...
@@ -37,146 +37,9 @@
},
{
"cell_type": "code",
"execution_count":
1
,
"execution_count":
null
,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>\n",
"\n",
"div.warn { \n",
" background-color: #fcf2f2;\n",
" border-color: #dFb5b4;\n",
" border-left: 5px solid #dfb5b4;\n",
" padding: 0.5em;\n",
" font-weight: bold;\n",
" font-size: 1.1em;;\n",
" }\n",
"\n",
"\n",
"\n",
"div.nota { \n",
" background-color: #DAFFDE;\n",
" border-left: 5px solid #92CC99;\n",
" padding: 0.5em;\n",
" }\n",
"\n",
"div.todo:before { content:url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI1My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mwuODI1NC0uODI1NEExLjA1NjgsMS4wNTY4LDAsMCwxLDM0LjMyNDUsOS4zNmwxLjE0NDIuODAxOUE3LjEzNTUsNy4xMzU1LDAsMCwxLDM3LjE4LDkuNDUxbC4yNDE2LTEuMzcyNGExLjA1NjgsMS4wNTY4LDAsMCwxLDEuMDQtLjg3NTdoMS4xNjc3YTEuMDU2OSwxLjA1NjksMCwwLDEsMS4wNC44NzU3bC4yNDE2LDEuMzcyNGE3LjEyNSw3LjEyNSwwLDAsMSwxLjcxMTIuNzExM0w0My43NjY2LDkuMzZBMS4wNTY5LDEuMDU2OSwwLDAsMSw0NS4xMjU1LDkuNDY3N1ptLTIuMDMsNi44OTg3QTQuMDQzMyw0LjA0MzMsMCwxLDAsMzkuMDUyMywyMC40MWgwQTQuMDQ2NSw0LjA0NjUsMCwwLDAsNDMuMDk1NSwxNi4zNjY0WiIgc3R5bGU9ImZpbGw6I2UxMjIyOSIvPjxwb2x5Z29uIHBvaW50cz0iMzkuNDEzIDM0Ljc1NyAzOS41MzcgMzQuNzU3IDM5LjY3NSAzNC43NTcgMzkuNjc1IDEwOS41MSAzOS41MzcgMTA5LjUxIDM5LjQxMyAxMDkuNTEgMzkuNDEzIDM0Ljc1NyAzOS40MTMgMzQuNzU3IiBzdHlsZT0iZmlsbDpub25lO3N0cm9rZTojOTk5O3N0cm9rZS1saW5lY2FwOnJvdW5kO3N0cm9rZS1taXRlcmxpbWl0OjEwO3N0cm9rZS13aWR0aDowLjMwODg1NDQ1MDU2MDE2MThweDtmaWxsLXJ1bGU6ZXZlbm9kZCIvPjwvc3ZnPg==);\n",
" float:left;\n",
" margin-right:20px;\n",
" margin-top:-20px;\n",
" margin-bottom:20px;\n",
"}\n",
"div.todo{\n",
" font-weight: bold;\n",
" font-size: 1.1em;\n",
" margin-top:40px;\n",
"}\n",
"div.todo ul{\n",
" margin: 0.2em;\n",
"}\n",
"div.todo li{\n",
" margin-left:60px;\n",
" margin-top:0;\n",
" margin-bottom:0;\n",
"}\n",
"\n",
"div .comment{\n",
" font-size:0.8em;\n",
" color:#696969;\n",
"}\n",
"\n",
"\n",
"\n",
"</style>\n",
"\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Override : Attribute [run_dir=./run/CelebA.001] with [./run/test-VAE8-3370]\n"
]
},
{
"data": {
"text/markdown": [
"**FIDLE 2020 - Practical Work Module**"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Version : 0.6.1 DEV\n",
"Notebook id : VAE8\n",
"Run time : Wednesday 6 January 2021, 19:47:34\n",
"TensorFlow version : 2.2.0\n",
"Keras version : 2.3.0-tf\n",
"Datasets dir : /home/pjluc/datasets/fidle\n",
"Run dir : ./run/test-VAE8-3370\n",
"Update keras cache : False\n",
"Save figs : True\n",
"Path figs : ./run/test-VAE8-3370/figs\n"
]
},
{
"data": {
"text/markdown": [
"<br>**FIDLE 2021 - VAE**"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Version : 1.2\n",
"TensorFlow version : 2.2.0\n",
"Keras version : 2.3.0-tf\n"
]
},
{
"data": {
"text/markdown": [
"<br>**FIDLE 2020 - DataGenerator**"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Version : 0.4.1\n",
"TensorFlow version : 2.2.0\n",
"Keras version : 2.3.0-tf\n"
]
}
],
"outputs": [],
"source": [
"import numpy as np\n",
"from skimage import io\n",
...
...
@@ -205,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count":
2
,
"execution_count":
null
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -231,7 +94,7 @@
},
{
"cell_type": "code",
"execution_count":
3
,
"execution_count":
null
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -272,17 +135,9 @@
},
{
"cell_type": "code",
"execution_count":
8
,
"execution_count":
null
,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train directory is : ./data/clusters-128x128\n"
]
}
],
"outputs": [],
"source": [
"# ---- Override parameters (batch mode) - Just forget this line\n",
"#\n",
...
...
@@ -304,17 +159,9 @@
},
{
"cell_type": "code",
"execution_count":
5
,
"execution_count":
null
,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Data generator is ready with : 379 batchs of 32 images, or 12155 images\n"
]
}
],
"outputs": [],
"source": [
"data_gen = DataGenerator(train_dir, 32, k_size=scale)\n",
"\n",
...
...
@@ -337,7 +184,7 @@
},
{
"cell_type": "code",
"execution_count":
6
,
"execution_count":
null
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -370,7 +217,7 @@
},
{
"cell_type": "code",
"execution_count":
7
,
"execution_count":
null
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -423,8 +270,8 @@
"metadata": {},
"source": [
"## Step 4 - Train\n",
"20'
on a CPU
\n",
"
1'12 on a GPU (V100, IDRIS)
"
"20'
for 10 epochs on a V100 (IDRIS)
\n",
"
...on a basic CPU, may be >40 hours !
"
]
},
{
...
...
%% Cell type:markdown id: tags:
<img
width=
"800px"
src=
"../fidle/img/00-Fidle-header-01.svg"
></img>
# <!-- TITLE --> [VAE8] - Variational AutoEncoder (VAE) with CelebA (small)
<!-- DESC -->
Variational AutoEncoder (VAE) with CelebA (small res. 128x128)
<!-- AUTHOR : Jean-Luc Parouty (CNRS/SIMaP) -->
## Objectives :
-
Build and train a VAE model with a large dataset in
**small resolution(>70 GB)**
-
Understanding a more advanced programming model with
**data generator**
The
[
CelebFaces Attributes Dataset (CelebA)
](
http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html
)
contains about 200,000 images (202599,218,178,3).
## What we're going to do :
-
Defining a VAE model
-
Build the model
-
Train it
-
Follow the learning process with Tensorboard
## Acknowledgements :
As before, thanks to
**François Chollet**
who is at the base of this example.
See : https://keras.io/examples/generative/vae
%% Cell type:markdown id: tags:
## Step 1 - Init python stuff
%% Cell type:code id: tags:
```
python
import
numpy
as
np
from
skimage
import
io
import
tensorflow
as
tf
from
tensorflow
import
keras
from
tensorflow.keras
import
layers
from
tensorflow.keras.callbacks
import
ModelCheckpoint
,
TensorBoard
import
os
,
sys
,
json
,
time
,
datetime
from
IPython.display
import
display
,
Image
,
Markdown
,
HTML
from
modules.data_generator
import
DataGenerator
from
modules.VAE
import
VAE
,
Sampling
from
modules.callbacks
import
ImagesCallback
,
BestModelCallback
sys
.
path
.
append
(
'
..
'
)
import
fidle.pwk
as
pwk
run_dir
=
'
./run/CelebA.001
'
# Output directory
datasets_dir
=
pwk
.
init
(
'
VAE8
'
,
run_dir
)
VAE
.
about
()
DataGenerator
.
about
()
```
%% Output
Override : Attribute [run_dir=./run/CelebA.001] with [./run/test-VAE8-3370]
**FIDLE 2020 - Practical Work Module**
Version : 0.6.1 DEV
Notebook id : VAE8
Run time : Wednesday 6 January 2021, 19:47:34
TensorFlow version : 2.2.0
Keras version : 2.3.0-tf
Datasets dir : /home/pjluc/datasets/fidle
Run dir : ./run/test-VAE8-3370
Update keras cache : False
Save figs : True
Path figs : ./run/test-VAE8-3370/figs
<br>**FIDLE 2021 - VAE**
Version : 1.2
TensorFlow version : 2.2.0
Keras version : 2.3.0-tf
<br>**FIDLE 2020 - DataGenerator**
Version : 0.4.1
TensorFlow version : 2.2.0
Keras version : 2.3.0-tf
%% Cell type:code id: tags:
```
python
# To clean run_dir, uncomment and run this next line
# ! rm -r "$run_dir"/images-* "$run_dir"/logs "$run_dir"/figs "$run_dir"/models ; rmdir "$run_dir"
```
%% Cell type:markdown id: tags:
## Step 2 - Get some data
Let's instantiate our generator for the entire dataset.
%% Cell type:markdown id: tags:
### 1.1 - Parameters
Uncomment the right lines according to the data you want to use
%% Cell type:code id: tags:
```
python
# ---- For tests
scale
=
0.3
image_size
=
(
128
,
128
)
enhanced_dir
=
'
./data
'
latent_dim
=
300
r_loss_factor
=
0.6
batch_size
=
64
epochs
=
15
# ---- Training with a full dataset
# scale = 1.
# image_size = (128,128)
# enhanced_dir = f'{datasets_dir}/celeba/enhanced'
# latent_dim = 300
# r_loss_factor = 0.6
# batch_size = 64
# epochs = 15
# ---- Training with a full dataset of large images
# scale = 1.
# image_size = (192,160)
# enhanced_dir = f'{datasets_dir}/celeba/enhanced'
# latent_dim = 300
# r_loss_factor = 0.6
# batch_size = 64
# epochs = 15
```
%% Cell type:markdown id: tags:
### 1.2 - Finding the right place
%% Cell type:code id: tags:
```
python
# ---- Override parameters (batch mode) - Just forget this line
#
pwk
.
override
(
'
scale
'
,
'
image_size
'
,
'
enhanced_dir
'
,
'
latent_dim
'
,
'
r_loss_factor
'
,
'
batch_size
'
,
'
epochs
'
)
# ---- the place of the clusters files
#
lx
,
ly
=
image_size
train_dir
=
f
'
{
enhanced_dir
}
/clusters-
{
lx
}
x
{
ly
}
'
print
(
'
Train directory is :
'
,
train_dir
)
```
%% Output
Train directory is : ./data/clusters-128x128
%% Cell type:markdown id: tags:
### 1.2 - Get a DataGenerator
%% Cell type:code id: tags:
```
python
data_gen
=
DataGenerator
(
train_dir
,
32
,
k_size
=
scale
)
print
(
f
'
Data generator is ready with :
{
len
(
data_gen
)
}
batchs of
{
data_gen
.
batch_size
}
images, or
{
data_gen
.
dataset_size
}
images
'
)
```
%% Output
Data generator is ready with : 379 batchs of 32 images, or 12155 images
%% Cell type:markdown id: tags:
## Step 3 - Build model
%% Cell type:markdown id: tags:
#### Encoder
%% Cell type:code id: tags:
```
python
inputs
=
keras
.
Input
(
shape
=
(
lx
,
ly
,
3
))
x
=
layers
.
Conv2D
(
32
,
3
,
strides
=
2
,
padding
=
"
same
"
,
activation
=
"
relu
"
)(
inputs
)
x
=
layers
.
Conv2D
(
64
,
3
,
strides
=
2
,
padding
=
"
same
"
,
activation
=
"
relu
"
)(
x
)
x
=
layers
.
Conv2D
(
64
,
3
,
strides
=
2
,
padding
=
"
same
"
,
activation
=
"
relu
"
)(
x
)
x
=
layers
.
Conv2D
(
64
,
3
,
strides
=
2
,
padding
=
"
same
"
,
activation
=
"
relu
"
)(
x
)
shape_before_flattening
=
keras
.
backend
.
int_shape
(
x
)[
1
:]
x
=
layers
.
Flatten
()(
x
)
x
=
layers
.
Dense
(
512
,
activation
=
"
relu
"
)(
x
)
z_mean
=
layers
.
Dense
(
latent_dim
,
name
=
"
z_mean
"
)(
x
)
z_log_var
=
layers
.
Dense
(
latent_dim
,
name
=
"
z_log_var
"
)(
x
)
z
=
Sampling
()([
z_mean
,
z_log_var
])
encoder
=
keras
.
Model
(
inputs
,
[
z_mean
,
z_log_var
,
z
],
name
=
"
encoder
"
)
encoder
.
compile
()
# encoder.summary()
```
%% Cell type:markdown id: tags:
#### Decoder
%% Cell type:code id: tags:
```
python
inputs
=
keras
.
Input
(
shape
=
(
latent_dim
,))
x
=
layers
.
Dense
(
np
.
prod
(
shape_before_flattening
))(
inputs
)
x
=
layers
.
Reshape
(
shape_before_flattening
)(
x
)
x
=
layers
.
Conv2DTranspose
(
64
,
3
,
strides
=
2
,
padding
=
"
same
"
,
activation
=
"
relu
"
)(
x
)
x
=
layers
.
Conv2DTranspose
(
64
,
3
,
strides
=
2
,
padding
=
"
same
"
,
activation
=
"
relu
"
)(
x
)
x
=
layers
.
Conv2DTranspose
(
64
,
3
,
strides
=
2
,
padding
=
"
same
"
,
activation
=
"
relu
"
)(
x
)
x
=
layers
.
Conv2DTranspose
(
32
,
3
,
strides
=
2
,
padding
=
"
same
"
,
activation
=
"
relu
"
)(
x
)
outputs
=
layers
.
Conv2DTranspose
(
3
,
3
,
padding
=
"
same
"
,
activation
=
"
sigmoid
"
)(
x
)
decoder
=
keras
.
Model
(
inputs
,
outputs
,
name
=
"
decoder
"
)
decoder
.
compile
()
# decoder.summary()
```
%% Cell type:markdown id: tags:
#### VAE
Our loss function is the weighted sum of two values.
`reconstruction_loss`
which measures the loss during reconstruction.
`kl_loss`
which measures the dispersion.
The weights are defined by:
`r_loss_factor`
:
`total_loss = r_loss_factor*reconstruction_loss + (1-r_loss_factor)*kl_loss`
if
`r_loss_factor = 1`
, the loss function includes only
`reconstruction_loss`
if
`r_loss_factor = 0`
, the loss function includes only
`kl_loss`
In practice, a value arround 0.5 gives good results here.
%% Cell type:code id: tags:
```
python
vae
=
VAE
(
encoder
,
decoder
,
r_loss_factor
)
vae
.
compile
(
optimizer
=
keras
.
optimizers
.
Adam
())
```
%% Cell type:markdown id: tags:
## Step 4 - Train
20'
on a CPU
1'12 on a GPU (V100, IDRIS)
20'
for 10 epochs on a V100 (IDRIS)
...on a basic CPU, may be >40 hours !
%% Cell type:markdown id: tags:
### 4.1 - Callbacks
%% Cell type:code id: tags:
```
python
x_draw
,
_
=
data_gen
[
0
]
data_gen
.
rewind
()
# ---- Callback : Images encoded
pwk
.
mkdir
(
run_dir
+
'
/images-encoded
'
)
filename
=
run_dir
+
'
/images-encoded/image-{epoch:03d}-{i:02d}.jpg
'
callback_images1
=
ImagesCallback
(
filename
,
x
=
x_draw
[:
5
],
encoder
=
encoder
,
decoder
=
decoder
)
# ---- Callback : Images generated
pwk
.
mkdir
(
run_dir
+
'
/images-generated
'
)
filename
=
run_dir
+
'
/images-generated/image-{epoch:03d}-{i:02d}.jpg
'
callback_images2
=
ImagesCallback
(
filename
,
x
=
None
,
nb_images
=
5
,
z_dim
=
latent_dim
,
encoder
=
encoder
,
decoder
=
decoder
)
# ---- Callback : Best model
pwk
.
mkdir
(
run_dir
+
'
/models
'
)
filename
=
run_dir
+
'
/models/best_model
'
callback_bestmodel
=
BestModelCallback
(
filename
)
# ---- Callback tensorboard
dirname
=
run_dir
+
'
/logs
'
callback_tensorboard
=
TensorBoard
(
log_dir
=
dirname
,
histogram_freq
=
1
)
callbacks_list
=
[
callback_images1
,
callback_images2
,
callback_bestmodel
,
callback_tensorboard
]
callbacks_list
=
[
callback_images1
,
callback_images2
,
callback_bestmodel
]
```
%% Cell type:markdown id: tags:
### 4.2 - Train it
%% Cell type:code id: tags:
```
python
pwk
.
chrono_start
()
history
=
vae
.
fit
(
data_gen
,
epochs
=
epochs
,
batch_size
=
batch_size
,
callbacks
=
callbacks_list
)
pwk
.
chrono_show
()
```
%% Cell type:markdown id: tags:
## Step 5 - About our training session
### 5.1 - History
%% Cell type:code id: tags:
```
python
pwk
.
plot_history
(
history
,
plot
=
{
"
Loss
"
:[
'
loss
'
,
'
r_loss
'
,
'
kl_loss
'
]},
save_as
=
'
01-history
'
)
```
%% Cell type:markdown id: tags:
### 5.2 - Reconstruction (input -> encoder -> decoder)
%% Cell type:code id: tags:
```
python
imgs
=
[]
labels
=
[]
for
epoch
in
range
(
1
,
epochs
,
1
):
for
i
in
range
(
5
):
filename
=
f
'
{
run_dir
}
/images-encoded/image-
{
epoch
:
03
d
}
-
{
i
:
02
d
}
.jpg
'
.
format
(
epoch
=
epoch
,
i
=
i
)
img
=
io
.
imread
(
filename
)
imgs
.
append
(
img
)
pwk
.
subtitle
(
'
Original images :
'
)
pwk
.
plot_images
(
x_draw
[:
5
],
None
,
indices
=
'
all
'
,
columns
=
5
,
x_size
=
2
,
y_size
=
2
,
save_as
=
'
02-original
'
)
pwk
.
subtitle
(
'
Encoded/decoded images
'
)
pwk
.
plot_images
(
imgs
,
None
,
indices
=
'
all
'
,
columns
=
5
,
x_size
=
2
,
y_size
=
2
,
save_as
=
'
03-reconstruct
'
)
pwk
.
subtitle
(
'
Original images :
'
)
pwk
.
plot_images
(
x_draw
[:
5
],
None
,
indices
=
'
all
'
,
columns
=
5
,
x_size
=
2
,
y_size
=
2
,
save_as
=
None
)
```
%% Cell type:markdown id: tags:
### 5.3 Generation (latent -> decoder)
%% Cell type:code id: tags:
```
python
imgs
=
[]
labels
=
[]
for
epoch
in
range
(
1
,
epochs
,
1
):
for
i
in
range
(
5
):
filename
=
f
'
{
run_dir
}
/images-generated/image-
{
epoch
:
03
d
}
-
{
i
:
02
d
}
.jpg
'
.
format
(
epoch
=
epoch
,
i
=
i
)
img
=
io
.
imread
(
filename
)
imgs
.
append
(
img
)
pwk
.
subtitle
(
'
Generated images from latent space
'
)
pwk
.
plot_images
(
imgs
,
None
,
indices
=
'
all
'
,
columns
=
5
,
x_size
=
2
,
y_size
=
2
,
save_as
=
'
04-encoded
'
)
```
%% Cell type:code id: tags:
```
python
pwk
.
end
()
```
%% Cell type:markdown id: tags:
---
<img
width=
"80px"
src=
"../fidle/img/00-Fidle-logo-01.svg"
></img>
...
...
This diff is collapsed.
Click to expand it.
VAE/batch_slurm.sh
+
1
−
1
View file @
584177e5
...
...
@@ -46,7 +46,7 @@ export FIDLE_OVERRIDE_VAE8_run_dir="./run/CelebA.$SLURM_JOB_ID"
export
FIDLE_OVERRIDE_VAE8_scale
=
"0.05"
export
FIDLE_OVERRIDE_VAE8_image_size
=
"(128,128)"
export
FIDLE_OVERRIDE_VAE8_enhanced_dir
=
'{datasets_dir}/celeba/enhanced'
export
FIDLE_OVERRIDE_VA
Z
8_r_loss_factor
=
"0.5"
export
FIDLE_OVERRIDE_VA
E
8_r_loss_factor
=
"0.5"
# ---- By default (no need to modify)
#
...
...
This diff is collapsed.
Click to expand it.
fidle/pwk.py
+
21
−
12
View file @
584177e5
...
...
@@ -93,7 +93,7 @@ def init(name=None, run_dir='./run'):
# ---- Hello world
#
display_md
(
'
**FIDLE 2020 - Practical Work Module**
'
)
display_md
(
'
<br>
**FIDLE 2020 - Practical Work Module**
'
)
print
(
'
Version :
'
,
config
.
VERSION
)
print
(
'
Notebook id :
'
,
notebook_id
)
print
(
'
Run time :
'
,
_start_time
.
strftime
(
"
%A %-d %B %Y, %H:%M:%S
"
))
...
...
@@ -210,7 +210,11 @@ def override(*names):
#
setattr
(
main
,
name
,
new_value
)
overrides
[
name
]
=
new_value
print
(
f
'
Override : Attribute [
{
name
}
=
{
value_old
}
] with [
{
new_value
}
]
'
)
if
len
(
overrides
)
>
0
:
display_md
(
'
**\*\* Overrided parameters : \*\***
'
)
for
name
,
value
in
overrides
.
items
():
print
(
f
'
{
name
:
20
s
}
:
{
value
}
'
)
return
overrides
...
...
@@ -765,22 +769,27 @@ def check_finished_file():
print
(
f
'
** Finished file should be at :
{
config
.
FINISHED_FILE
}
\n
'
)
return
False
return
True
def
reset_finished_file
():
if
check_finished_file
()
==
False
:
return
data
=
{}
# ---- Save it
with
open
(
config
.
FINISHED_FILE
,
'
wt
'
)
as
fp
:
json
.
dump
(
data
,
fp
,
indent
=
4
)
print
(
f
'
Finished file has been reset.
\n
'
)
def
reset_finished_file
(
verbose
=
False
):
try
:
data
=
{}
with
open
(
config
.
FINISHED_FILE
,
'
wt
'
)
as
fp
:
json
.
dump
(
data
,
fp
,
indent
=
4
)
if
verbose
:
print
(
f
'
Finished file has been reset.
\n
'
)
except
:
print
(
f
'
\n
**Warning : cannot reset finished file (
{
config
.
FINISHED_FILE
}
)
\n
'
)
return
False
return
True
def
update_finished_file
(
start
=
False
,
end
=
False
):
# ---- No writable finished file ?
if
check_finished_file
()
is
False
:
return
if
not
os
.
access
(
config
.
FINISHED_FILE
,
os
.
W_OK
):
done
=
reset_finished_file
()
if
not
done
:
return
# ---- Load it
with
open
(
config
.
FINISHED_FILE
)
as
fp
:
data
=
json
.
load
(
fp
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment