(wh, xc, size=10)
| 154 | |
| 155 | |
| 156 | def log_txt_as_img(wh, xc, size=10): |
| 157 | # wh a tuple of (width, height) |
| 158 | # xc a list of captions to plot |
| 159 | b = len(xc) |
| 160 | txts = list() |
| 161 | for bi in range(b): |
| 162 | txt = Image.new("RGB", wh, color="white") |
| 163 | draw = ImageDraw.Draw(txt) |
| 164 | font = ImageFont.truetype("data/DejaVuSans.ttf", size=size) |
| 165 | nc = int(40 * (wh[0] / 256)) |
| 166 | if isinstance(xc[bi], list): |
| 167 | text_seq = xc[bi][0] |
| 168 | else: |
| 169 | text_seq = xc[bi] |
| 170 | lines = "\n".join(text_seq[start : start + nc] for start in range(0, len(text_seq), nc)) |
| 171 | |
| 172 | try: |
| 173 | draw.text((0, 0), lines, fill="black", font=font) |
| 174 | except UnicodeEncodeError: |
| 175 | print("Cant encode string for logging. Skipping.") |
| 176 | |
| 177 | txt = np.array(txt).transpose(2, 0, 1) / 127.5 - 1.0 |
| 178 | txts.append(txt) |
| 179 | txts = np.stack(txts) |
| 180 | txts = torch.tensor(txts) |
| 181 | return txts |
| 182 | |
| 183 | |
| 184 | def partialclass(cls, *args, **kwargs): |
no test coverage detected